我有一个查询,它使用BETWEEN
来显示两个日期之间的记录。我的查询需要显示特定日期之间arrival_date
和departure_date
的记录。但查询以某种方式显示所有记录。
列类型为DATE
。
SELECT DISTINCT art.* FROM accommodation_room_types art
INNER JOIN accommodation_rooms ar ON art.id = ar.room_type
INNER JOIN accommodation a ON art.accommodation = a.id
WHERE a.id = 13 AND NOT EXISTS
(
SELECT 1 FROM booked_rooms br INNER JOIN booking b ON br.booking = b.id
WHERE br.room = ar.id
AND
(
b.arrival_date BETWEEN '2017-12-16' AND '2018-04-16'
)
OR
(
b.departure_date BETWEEN '2017-12-16' AND '2018-04-16'
)
)
即使我写BETWEEN 'asd' AND 'asd'
,它仍会显示所有记录,并且不会给出任何格式错误。
我的查询在两个特定日期之间显示记录是错误的吗?
答案 0 :(得分:0)
我不知道你的逻辑是对还是错,但你的语法并没有按你的意愿行事。我建议:
WHERE a.id = 13 AND
NOT EXISTS (SELECT 1
FROM booked_rooms br INNER JOIN
booking b
ON br.booking = b.id
WHERE br.room = ar.id AND
(b.arrival_date BETWEEN '2017-12-16' AND '2018-04-16' OR
b.departure_date BETWEEN '2017-12-16' AND '2018-04-16'
)
)
令我感到震惊的是,查询中的所有空白区域都很难看出您的逻辑被写为:A AND B OR C
。你的意图(大概)是A AND (B OR C)
。
答案 1 :(得分:-1)
将1
替换为*
您编写查询的方式,无论条件如何,它始终返回1。而且,这是完全合法的。