仅当该房间在两个日期之间有保留(prenotazione)时,我才想打印该房间的编号(节),以便在设置带有结果的变量时在php中显示错误消息。问题是我的查询似乎要检查是否有房间在这些日期之间设置了保留,并始终提供所请求的任何房间的输出。
SELECT stanze.num_stanza
FROM stanze, prenotazioni
WHERE prenotazioni.num_stanza=stanze.num_stanza
AND prenotazioni.check_in
BETWEEN '20190615'
AND '20190620'
OR prenotazioni.check_out
BETWEEN '20190615'
AND '20190620'
AND stanze.num_stanza='100'
答案 0 :(得分:0)
通常,像x OR y AND z这样的WHERE子句并不是很好。尝试(x或y)和z。否则,如果x为true,则无论z是什么,它都会选择。
例如
WHERE prenotazioni.num_stanza = stanze.num_stanza
AND (prenotazioni.check_in BETWEEN '20190615' AND '20190620' OR prenotazioni.check_out BETWEEN '20190615' AND '20190620')
AND stanze.num_stanza = '100'
答案 1 :(得分:0)
不清楚您想要的是什么-全部重叠或部分重叠。
无论哪种方式,您都可以使用EXISTS
。以下是部分重叠的信息:
SELECT s.num_stanza
FROM stanze s
WHERE EXISTS (SELECT 1
FROM prenotazioni p
WHERE p.num_stanza = s.num_stanza AND
p.check_in < '20190620' AND
p.check_out >= '20190615'
) AND
s.num_stanza = 100 -- looks like number so it probably is