我正在制作一个会议室预订系统,您在该系统中选择一个特定的时间段,并且应该在那个时候获得可用的房间。 某些表的名称是法文,因此在需要时我将提供()之间的含义。 我的问题是,当我选择时间时,甚至没有可用的房间。而且我认为我的查询存在一些问题。 因此,我有一个预订桌,教授桌和萨尔(法语中的指间)表。 预订表包含以下列:idReservation,dateReservation,hDebut(这是会议的开始时间), hFin(结束时间),idSalle(房间ID)
以下是创建表查询:如果您不理解某些列名(例如我用法语说的那样),请发表评论。
CREATE TABLE `reservation` (
`idReservation` int(11) NOT NULL,
`dateReservation` date DEFAULT NULL,
`hDebut` time NOT NULL,
`hFin` time NOT NULL,
`dateModificationReservation` date DEFAULT NULL,
`motifReservation` varchar(50) NOT NULL,
`idSalle` int(11) NOT NULL,
`idProf` int(11) NOT NULL,
`etatreserv` int(10) NOT NULL
)
etatreserv(等待2个,拒绝1个,接受0个)代表房间的状态,因为用户发送了一个预定时间的房间预订请求,并且管理员可以选择接受/拒绝房间可用
INSERT INTO `reservation`(`idReservation`, `dateReservation`, `hDebut`, `hFin`, `dateModificationReservation`, `motifReservation`, `idSalle`, `idProf`, `etatreserv`) VALUES (1,'2019-06-14','15:00:00','18:00:00','2019-06-09','administrative meeting',1,12,2)
CREATE TABLE `salle` (
`idSalle` int(11) NOT NULL,
`intitule` varchar(40) NOT NULL,
`NumeroSalle` int(255) NOT NULL
)
INSERT INTO `salle` (`idSalle`, `intitule`, `NumeroSalle`) VALUES
(1, 'salle de reunion 1', 1),
(10, 'salle 18', 18);
CREATE TABLE `professeur` (
`idProf` int(11) NOT NULL,
`nomProf` varchar(50) NOT NULL,
`prenomProf` varchar(50) NOT NULL,
`tel` int(10) UNSIGNED NOT NULL,
`email` varchar(255) NOT NULL,
`profpwd` varchar(255) NOT NULL,
`dateAddProf` date NOT NULL,
`dateModificationProf` date NOT NULL,
`idDepart` int(11) NOT NULL
)
INSERT INTO `professeur` (`idProf`, `nomProf`, `prenomProf`, `tel`, `email`, `profpwd`, `dateAddProf`, `dateModificationProf`, `idDepart`) VALUES
(12, 'ddd', 'dd', 444, 'email@email.com', 'pwd', '2019-06-05', '2019-06-05', 5);
我已经尝试过的方法:
SELECT * FROM reservation left OUTER JOIN salle on reservation.idSalle = salle.idSalle WHERE (
(reservation.dateReservation IS NULL)
OR
(reservation.dateReservation != ':dateReservation')
OR
((reservation.dateReservation = ':dateReservation') AND (reservation.hDebut NOT BETWEEN ':hDebut' AND ':hFin') AND (reservation.hFin NOT BETWEEN ':hDebut' AND ':hFin'))
OR
((reservation.dateReservation = ':dateReservation') AND (reservation.hDebut >':hDebut' AND reservation.hFin <':hFin'))
)
然后我尝试了一个更简化的版本:
dateReservation = ':dateReservation' AND
(hDebut BETWEEN ':hDebut' AND ':hFin' OR hFin BETWEEN ':hDebut' AND ':hFin' OR
(hDebut < ':hDebut' AND hFin > ':hFin'))))```
example :
salle table
idSalle | name
1 |salle de reunion 1
10 |salle18
3 |salle3
reservation table
idReservation|dateReservation|hDebut |hFin |idSalle
1 |2019-06-14 |15:00:00 |18:00:00 |1
if for example i choose dateReservation = 2019-06-14, hDebut=16:00:00,
hFin = 17:00:00 then all i should see are : salle2 and salle3
PS : I'm new to stackoverflow and the syntax highlighting confused me a bit, so I'm sorry if my code doesn't show up as it should.
答案 0 :(得分:1)
DROP TABLE IF EXISTS salle;
CREATE TABLE salle
(salle_id SERIAL PRIMARY KEY
,name VARCHAR(12) UNIQUE
);
INSERT INTO salle VALUES
(1,'salle1'),
(2,'salle2'),
(3,'salle3');
DROP TABLE IF EXISTS reservation ;
CREATE TABLE reservation
(reservation_id SERIAL PRIMARY KEY
,reservation_from DATETIME NOT NULL
,reservation_to DATETIME NULL
,salle_id INT NOT NULL
);
INSERT INTO reservation VALUES
(1,'2019-06-14 15:00:00','2019-06-14 18:00:00',1);
SELECT s.*
FROM salle s
LEFT
JOIN reservation r
ON r.salle_id = s.salle_id
AND reservation_from <= '2019-06-14 17:00:00'
AND reservation_to >= '2019-06-14 16:00:00'
WHERE r.reservation_id IS NULL;
+----------+--------+
| salle_id | name |
+----------+--------+
| 2 | salle2 |
| 3 | salle3 |
+----------+--------+