SQL在哪里查询IN条件的第一行

时间:2018-06-11 09:41:36

标签: mysql sql where

我有这个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和release_days< = 2"

参数,但我不知道如何做。

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

  

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

但这不是正确的查询。

UDPATE

 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

2 个答案:

答案 0 :(得分:1)

这是您的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')  

答案 1 :(得分:0)

  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-10 00:00:00', '2018-06-11 00:00:00')
    GROUP BY
      roomTypeDay2.room_type_id
    HAVING
      COUNT(roomTypeDay2.room_type_id) = 3
    union
    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 = '2018-06-09 00:00:00'
      and roomTypeDay2.min_night_stay <= 3
      AND roomTypeDay2.release_days <= 2
    GROUP BY
      roomTypeDay2.room_type_id
    HAVING
      COUNT(roomTypeDay2.room_type_id) = 3
  ) disponible