I have this SQL query:

```
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

```

This works fine, but now, I want to filter if the first date row (2018-06-09 00:00:00) have 

> 
> "min\_night\_stay <= 3 and release\_days <=2"
> 
> 
> 

parameters, but I don't know how doing it. 

I try adding these lines to a query:

> 
> AND (roomTypeDay2.date = '2018-06-09 00:00:00' and
>  roomTypeDay2.min\_night\_stay <= 3 AND roomTypeDay2.release\_days <= 2)
> 
> 
> 

But this is not correct query. 

**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

```

---

Solution
========

```
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

```