SQL где запрос к первой строке условия IN - PullRequest
0 голосов
/ 11 июня 2018

У меня есть 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" </p>

, но я не знаю, как это сделать.

Я пытаюсь добавить эти строки в запрос:

AND (roomTypeDay2.date = '2018-06-09 00:00:00' и roomTypeDay2.min_night_stay <= 3 ANDroomTypeDay2.release_days <= 2) </p>

Но это неверный запрос.

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 голосов
/ 11 июня 2018

Это ваше WHERE предложение:

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

Теперь для первого свидания вам нужны дополнительные критерии.Для этого используйте AND и OR с круглыми скобками:

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')  
0 голосов
/ 11 июня 2018
  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
...