我正在为办公室开发一个网络系统,家里的病人可以在系统中注册,这样可以预约特定类型的检查。
当患者请求预约时,他会放置他的个人数据,并且还会放置所需的测试类型。我在mysql中有一个名为"考试类型"其中发现了5种类型的考试,并附有练习方式,也就是说,每种类型的考试都是在每天指定的时间间隔内实现的。
到目前为止,一切都进展顺利,我想限制可以要求进行一天测试的患者数量,我无法成功实现。
办公室每天只允许20次预约,但每种类型的检查最多只允许5名患者。
我想知道,您怎么能限制每天只有5名患者可以要求进行该类检查?
我不知道如何限制系统的这一部分,我希望他们能帮助我。我感谢任何支持我的人。
答案 0 :(得分:1)
请考虑以下事项:
DROP TABLE IF EXISTS bookings;
CREATE TABLE bookings
(booking_id INT NOT NULL AUTO_INCREMENT PRIMARY KEY
,user_id INT NOT NULL
,type INT NOT NULL
,booking_date DATE NOT NULL
,UNIQUE(user_id,booking_date)
);
INSERT INTO bookings (user_id, type, booking_date)
SELECT 1
, 1
, '2017-08-05'
FROM (SELECT 1) x
WHERE (SELECT COUNT(*)
FROM bookings
WHERE booking_date = '2017-08-05'
AND type = 1) < 5;
Query OK, 1 row affected (0.00 sec)
Records: 1 Duplicates: 0 Warnings: 0
INSERT INTO bookings (user_id, type, booking_date)
SELECT 2
, 1
, '2017-08-05'
FROM (SELECT 1) x
WHERE (SELECT COUNT(*)
FROM bookings
WHERE booking_date = '2017-08-05'
AND type = 1) < 5;
Query OK, 1 row affected (0.00 sec)
Records: 1 Duplicates: 0 Warnings: 0
INSERT INTO bookings (user_id, type, booking_date)
SELECT 3
, 1
, '2017-08-05'
FROM (SELECT 1) x
WHERE (SELECT COUNT(*)
FROM bookings
WHERE booking_date = '2017-08-05'
AND type = 1) < 5;
Query OK, 1 row affected (0.00 sec)
Records: 1 Duplicates: 0 Warnings: 0
INSERT INTO bookings (user_id, type, booking_date)
SELECT 4
, 1
, '2017-08-05'
FROM (SELECT 1) x
WHERE (SELECT COUNT(*)
FROM bookings
WHERE booking_date = '2017-08-05'
AND type = 1) < 5;
Query OK, 1 row affected (0.00 sec)
Records: 1 Duplicates: 0 Warnings: 0
INSERT INTO bookings (user_id, type, booking_date)
SELECT 5
, 1
, '2017-08-05'
FROM (SELECT 1) x
WHERE (SELECT COUNT(*)
FROM bookings
WHERE booking_date = '2017-08-05'
AND type = 1) < 5;
Query OK, 1 row affected (0.00 sec)
Records: 1 Duplicates: 0 Warnings: 0
SELECT * FROM bookings;
+------------+---------+------+--------------+
| booking_id | user_id | type | booking_date |
+------------+---------+------+--------------+
| 1 | 1 | 1 | 2017-08-05 |
| 2 | 2 | 1 | 2017-08-05 |
| 3 | 3 | 1 | 2017-08-05 |
| 4 | 4 | 1 | 2017-08-05 |
| 5 | 5 | 1 | 2017-08-05 |
+------------+---------+------+--------------+
5 rows in set (0.00 sec)
INSERT INTO bookings (user_id, type, booking_date)
SELECT 6
, 1
, '2017-08-05'
FROM (SELECT 1) x
WHERE (SELECT COUNT(*)
FROM bookings
WHERE booking_date = '2017-08-05'
AND type = 1) < 5;
Query OK, 0 rows affected (0.00 sec)
Records: 0 Duplicates: 0 Warnings: 0
SELECT * FROM bookings;
+------------+---------+------+--------------+
| booking_id | user_id | type | booking_date |
+------------+---------+------+--------------+
| 1 | 1 | 1 | 2017-08-05 |
| 2 | 2 | 1 | 2017-08-05 |
| 3 | 3 | 1 | 2017-08-05 |
| 4 | 4 | 1 | 2017-08-05 |
| 5 | 5 | 1 | 2017-08-05 |
+------------+---------+------+--------------+
5 rows in set (0.00 sec)