gpt4 book ai didi

mysql - SQL where查询到IN条件的第一行

转载 作者:行者123 更新时间:2023-11-29 02:41:35 24 4
gpt4 key购买 nike

我有这个 SQL 查询:

SELECT (CASE WHEN count(_days) > 0 THEN 'yes' ELSE 'No' END) as availability 
FROM (
SELECT count(roomTypeDay2.room_type_id) as _days
FROM room_type_day as roomTypeDay2
LEFT JOIN room_type as roomType2 on roomTypeDay2.room_type_id = roomType2.id
WHERE roomType2.accommodation_id=3
AND roomTypeDay2.date IN ( '2018-06-09 00:00:00','2018-06-10 00:00:00','2018-06-11 00:00:00')
GROUP BY roomTypeDay2.room_type_id
HAVING COUNT(roomTypeDay2.room_type_id) = 3
) as disponible

这工作正常,但现在,我想过滤第一个日期行 (2018-06-09 00:00:00) 是否有

"min_night_stay <= 3 and release_days <=2"

参数,但我不知道怎么做。

我尝试将这些行添加到查询中:

AND (roomTypeDay2.date = '2018-06-09 00:00:00' and roomTypeDay2.min_night_stay <= 3 AND roomTypeDay2.release_days <= 2)

但这不是正确的查询。

更新

 SELECT (CASE WHEN count(_days) > 0 THEN 'yes' ELSE 'No' END) as availability 
FROM (
SELECT count(roomTypeDay2.room_type_id) as _days
FROM room_type_day as roomTypeDay2
LEFT JOIN room_type as roomType2 on roomTypeDay2.room_type_id = roomType2.id
AND roomType2.accommodation_id=3
WHERE roomTypeDay2.date IN ( '2018-06-09 00:00:00','2018-06-10 00:00:00','2018-06-11 00:00:00')
GROUP BY roomTypeDay2.room_type_id
HAVING COUNT(roomTypeDay2.room_type_id) = 3
) as disponible

解决方案

SELECT (CASE WHEN count(_days) > 0 THEN 'yes' ELSE 'No' END) as availability 
FROM (
SELECT count(roomTypeDay2.room_type_id) as _days
FROM room_type_day as roomTypeDay2
LEFT JOIN room_type as roomType2 on roomTypeDay2.room_type_id = roomType2.id
WHERE roomType2.accommodation_id=3
AND roomTypeDay2.num_rooms_available > 0
AND (roomTypeDay2.date = '2018-06-12 00:00:00' AND roomTypeDay2.min_night_stay <= 2 AND roomTypeDay2.release_days <= 2 )
OR roomTypeDay2.date IN ( '2018-06-13 00:00:00','2018-06-14 00:00:00')
GROUP BY roomTypeDay2.room_type_id
HAVING COUNT(roomTypeDay2.room_type_id) = 3
) as disponible

最佳答案

这是您的 WHERE 子句:

WHERE roomTypeDay2.date IN ('2018-06-09 00:00:00',
'2018-06-10 00:00:00',
'2018-06-11 00:00:00')

现在对于第一次约会,您需要额外的标准。为此,使用带括号的 ANDOR:

WHERE 
(
roomTypeDay2.date = '2018-06-09 00:00:00'
AND
roomTypeDay2.min_night_stay <= 3
AND
roomTypeDay2.release_days <= 2
)
OR
roomTypeDay2.date IN ('2018-06-10 00:00:00','2018-06-11 00:00:00')

关于mysql - SQL where查询到IN条件的第一行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50794690/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com