为什么这个sql给出minimum
字段的表为null。此外,当A
在给定日期范围之间没有数据时,它会向表格提供所有rooms
minimum
为空
SELECT `rooms`.*,A.`minimum`
FROM (
SELECT `room_id`, min(`available_rooms`) AS `minimum`
FROM `room_bookings`
WHERE `date` BETWEEN '2014-02-01' and '2014-02-10'
GROUP BY `room_id`) as A
INNER JOIN `rooms` on `rooms`.`room_id`=A.`room_id`
WHERE `rooms`.`location`='kathmandu'
AND `rooms`.`status`=1
AND A.`minimum`!=NULL
答案 0 :(得分:2)
SELECT `rooms`.*,A.`minimum`
FROM (
SELECT `room_id`, min(`available_rooms`) AS `minimum`
FROM `room_bookings`
WHERE `date` BETWEEN '2014-02-01' and '2014-02-10'
GROUP BY `room_id` having minimum > 0) as A
INNER JOIN `rooms` on `rooms`.`room_id`=A.`room_id`
WHERE `rooms`.`location`='kathmandu'
AND `rooms`.`status`=1
答案 1 :(得分:2)
SELECT `rooms`.*,A.`minimum`
FROM (
SELECT `room_id`, min(`available_rooms`) AS `minimum`
FROM `room_bookings`
WHERE `date` BETWEEN '2014-02-01' and '2014-02-10'
GROUP BY `room_id`) as A
INNER JOIN `rooms` on `rooms`.`room_id`=A.`room_id`
WHERE `rooms`.`location`='kathmandu'
AND `rooms`.`status`=1
AND A.`minimum` is not NULL
这将是正确的语法(将!=
更改为is not
)
答案 2 :(得分:1)
尝试使用IS NOT NULL
SELECT `rooms`.*,A.`minimum`
FROM (
SELECT `room_id`, min(`available_rooms`) AS `minimum`
FROM `room_bookings`
WHERE `date` BETWEEN '2014-02-01' and '2014-02-10'
GROUP BY `room_id`) as A
INNER JOIN `rooms` on `rooms`.`room_id`=A.`room_id`
WHERE `rooms`.`location`='kathmandu'
AND `rooms`.`status`=1
AND A.`minimum` IS NOT NULL
答案 3 :(得分:1)
试试这个:
SELECT r.*, MIN(rb.available_rooms) minimum
FROM rooms r
INNER JOIN room_bookings rb ON r.room_id = rb.room_id AND rb.date BETWEEN '2014-02-01' AND '2014-02-10'
WHERE r.location = 'kathmandu' AND r.status = 1
GROUP BY r.room_id HAVING minimum IS NOT NULL