为什么此查询没有考虑“c.max_no_people< = $ max_guests”的Where子句?
SELECT DISTINCT a.id, b.post_id, c.max_no_people
FROM wp_dopbsp_reservations a
INNER JOIN wp_dopbsp_calendars b
ON ( b.id = a.calendar_id)
INNER JOIN wp_dopbsp_settings c
ON ( b.id = c.calendar_id )
WHERE (a.check_out >= \"$check_in\"
AND a.check_in < \"$check_out\"
OR a.check_out <= \"$check_in\"
AND a.check_in > \"$check_out\")
AND (c.max_no_people <= $max_guests)
AND (a.status = 'approved')
提前致谢。
答案 0 :(得分:1)
试试这个:
SELECT DISTINCT a.id, b.post_id, c.max_no_people
FROM wp_dopbsp_reservations a
INNER JOIN wp_dopbsp_calendars b
ON ( b.id = a.calendar_id)
INNER JOIN wp_dopbsp_settings c
ON ( b.id = c.calendar_id )
WHERE a.check_out >= \"$check_in\"
AND (a.check_in < \"$check_out\"
OR a.check_out <= \"$check_in\")
AND a.check_in > \"$check_out\"
AND (c.max_no_people <= $max_guests)
AND (a.status = 'approved')
这是因为你在不使用括号的情况下给出了OR语句。所以它只考虑OR语句。
答案 1 :(得分:1)
嗯,我认为问题出在这里
(a.check_out >= \"$check_in\"
AND a.check_in < \"$check_out\"
OR a.check_out <= \"$check_in\"
AND a.check_in > \"$check_out\")
不应该:
(
(a.check_out >= \"$check_in\"
AND a.check_in < \"$check_out\") OR
( a.check_out <= \"$check_in\"
AND a.check_in > \"$check_out\")
)
同样逃避 $ max_guests ,如(int)$ max_guests