这是我的查询,
SELECT
`h`.`hotel_id`,
(
SELECT COUNT(room_id)
FROM
`abserve_hotel_rooms` AS `rm`
WHERE
`rm`.`adults_count` >= "1" AND `rm`.`room_count` >= "1" AND "Available" = IF(
check_in_time = '2016-03-15',
'Unavailable',
(
IF(
'2016-03-15' > check_in_time,
(
IF(
'2016-03-15' < check_out_time,
'Unavailable',
'Available'
)
),
(
IF(
'2016-03-22' > check_in_time,
'Unavailable',
'Available'
)
)
)
)
) AND `room_prize` BETWEEN '174' AND '600' AND `rm`.`hotel_id` = `h`.`hotel_id`
) AS `avail_room_count`,
(
SELECT MIN(room_prize)
FROM
`abserve_hotel_rooms` AS `rm`
WHERE
`rm`.`adults_count` >= "1" AND `rm`.`room_count` >= "1" AND "Available" = IF(
check_in_time = '2016-03-15',
'Unavailable',
(
IF(
'2016-03-15' > check_in_time,
(
IF(
'2016-03-15' < check_out_time,
'Unavailable',
'Available'
)
),
(
IF(
'2016-03-22' > check_in_time,
'Unavailable',
'Available'
)
)
)
)
) AND `room_prize` BETWEEN '174' AND '600' AND `rm`.`hotel_id` = `h`.`hotel_id`
) AS `min_val`
FROM
`abserve_hotels` AS `h`
WHERE
1 AND `city` = "madurai" AND `country` = "india"
它完全返回我的表abserve_hotels
中的一个列值hotel_id
,其中包含额外的两个别名列,例如avail_room_count
和min_val
..
我在子查询中写了这些......
在这里,我必须检查条件WHERE min_val IS NOT NULL
.i.e;如果min_val
具有NULL值,我必须限制它
我该怎么做..
这是我的表
hotel_id avail_room_count min_val
1 0 NULL
2 0 NULL
这里我需要限制这些NULL值..
有人请帮帮我..
答案 0 :(得分:1)
在最后添加一个HAVING子句:
HAVING min_val IS NOT NULL
WHERE之后的新查询如下:
WHERE
1 AND `city` = "madurai" AND `country` = "india"
HAVING min_val IS NOT NULL
答案 1 :(得分:1)
您的查询过于复杂,可以简化:
SELECT
列表(MIN
与COUNT
)之外,两个相关的子查询完全相同,因此它们可以合并为一个; 事实上,您可以使用以下查询完成所需的所有操作:
SELECT h.hotel_id,
COUNT(rm.room_id) as avail_room_count,
MIN(rm.room_prize) AS min_val
FROM abserve_hotels AS h
INNER JOIN abserve_hotel_rooms AS rm
ON rm.hotel_id = h.hotel_id
WHERE h.city = "madurai"
AND h.country = "india"
AND rm.adults_count >= 1
AND rm.room_count >= 1
AND rm.room_prize BETWEEN 174 AND 600
AND ( rm.check_in_time >= '2016-03-22'
OR rm.check_out_time <= '2016-03-15'
OR rm.check_in_time IS NULL)
GROUP BY h.hotel_id
由于INNER JOIN
至少需要一次匹配,因此您可以确保min_val
永远不会是NULL
。
检查可用性就像:
( rm.check_in_time >= '2016-03-22'
OR rm.check_out_time <= '2016-03-15'
OR rm.check_in_time IS NULL)
该条件的三个部分意味着:
在所有三种情况下,房间都可以预订相关的一周。