2

This is my query:

$result = mysql_query("
           Select count(RoomType) AS Available_Rooms, RoomType, RoomRate 
           FROM tb_rooms 
           WHERE RoomNumber NOT IN (Select tb_roomsreserved.RoomNumber 
                                    From tb_roomsreserved 
                                    LEFT JOIN tb_reserved on tb_roomsreserved.reserved_id = tb_reserved.reserved_id 
                                    WHERE $arrival < tb_reserved.Departure AND $departure >tb_reserved.Arrival 
                                    UNION Select tb_roomsguest.RoomNumber From tb_roomsguest 
                                          LEFT JOIN tb_guest on tb_roomsguest.id = tb_guest.id 
                                          WHERE  $arrival < tb_guest.Departure AND $departure >tb_guest.Arrival 
                                          AND tb_guest.Status = 'Approved' 
                                          UNION Select tb_roomscheckin.RoomNumber From tb_roomscheckin 
                                                LEFT JOIN tb_checkin on tb_roomscheckin.checkin_id = tb_checkin.checkin_id 
                                                WHERE  $arrival < tb_checkin.Departure AND $departure >tb_checkin.Arrival
                                    ) 
       GROUP BY RoomType") or die (mysql_error());

This is my table:

echo '<table style = "margin-bottom: 2.5%;" width = "90%">';
    while($row = mysql_fetch_array($result)){   
            echo '<tr><td align = "center">';
            echo $row['Available_Rooms'];
            echo '</td><td align = "center">';
            echo $row['RoomType'];
            echo '</td><td align = "center">';
            echo 'Available';
            echo '</td></tr>';
        }

    echo '</table>';

How can I echo those rooms that are not available? My query show only rooms which are available.

Normally my table looks like this when all rooms are available.

Available_Rooms          RoomType           Availability
       5                 AtticRoom            Available
       4                 Deluxe               Available
       4                 FamilyRoom           Available
       13                StandardRoom         Available

But when Deluxe And Family Rooms Hits 0 available rooms. The outputs are these on.

Available_Rooms          RoomType           Availability
       5                 AtticRoom            Available
       13                StandardRoom         Available

I have been thinking on how can I do that.

i ended up having this output.

Available_Rooms          RoomType             RoomRate
       5                 AtticRoom              2000
       13                StandardRoom           2500

my tb_rooms contains

RoomNumber       RoomType         RoomRate
201             FamilyRoom         3500
202            StandardRoom        2500
203            StandardRoom        2500
204            StandardRoom        2500
205            StandardRoom        2500
206            StandardRoom        2500
207            StandardRoom        2500
208            StandardRoom        2500
209            StandardRoom        2500
210            StandardRoom        2500
211            Deluxe              2300
212            FamilyRoom          3500
214            Deluxe              2300
301            FamilyRoom          3500
302            FamilyRoom          3500
303            StandardRoom        2500
304            StandardRoom        2500
305            StandardRoom        2500
306            StandardRoom        2500
307            StandardRoom        2500
308            Deluxe              2300
309            Deluxe              2300
401            AtticRoom           2000
402            AtticRoom           2000
403            AtticRoom           2000
404            AtticRoom           2000
405            AtticRoom           2000
kier
  • 51
  • 6

1 Answers1

0

Use the below query

$result = mysql_query("Select (CASE WHEN count(RoomType) > 0 THEN count(RoomType) ELSE 0 END) AS Available_Rooms, RoomType, RoomRate 
                   FROM tb_rooms 
                   WHERE RoomNumber NOT IN (
                         Select tb_roomsreserved.RoomNumber 
                         FROM tb_roomsreserved 
                         LEFT JOIN tb_reserved on tb_roomsreserved.reserved_id = tb_reserved.reserved_id 
                         WHERE $arrival < tb_reserved.Departure AND $departure >tb_reserved.Arrival 
                         UNION Select tb_roomsguest.RoomNumber 
                               FROM tb_roomsguest 
                               LEFT JOIN tb_guest on tb_roomsguest.id = tb_guest.id 
                               WHERE  $arrival < tb_guest.Departure AND $departure >tb_guest.Arrival AND tb_guest.Status = 'Approved' 
                               UNION Select tb_roomscheckin.RoomNumber 
                                     FROM tb_roomscheckin 
                                     LEFT JOIN tb_checkin on tb_roomscheckin.checkin_id = tb_checkin.checkin_id 
                                     WHERE  $arrival < tb_checkin.Departure AND $departure >tb_checkin.Arrival
                    )
                    GROUP BY RoomType") or die (mysql_error());
Santosh Jagtap
  • 995
  • 8
  • 17