开发者

Combining two queries in mySQL

I'm trying to combine the results of two queries. I'm not very proficient in mysql so I'm here for some help.

The first query is as follows:

select count(roomtypeid) as bookedrooms, day 
from event_guest_hotel 
where hotelid = 开发者_Go百科1 and roomtypeid = 1 
 group by day;

This returns:

Combining two queries in mySQL

The second query:

SELECT ehr.reservationid, ehr.day, h.name AS hotelname, 
ehr.totalrooms as requested_rooms, r.name AS roomname
            FROM event_hotel_reservation ehr 
            INNER JOIN hotel_room_type r 
            ON ehr.roomtypeid = r.roomtypeid 
            INNER JOIN hotel h
            ON ehr.hotelid = h.hotelid  
            WHERE totalRooms != 0 
            AND reservationID = '1'

This returns:

Combining two queries in mySQL

Can I combine the first query with the second one, so I get the results of the first one in another resultcolumn next to 'roomname'? That way I know how many rooms are already booked and how many were originally requested from one single query.


Try:

SELECT ehr.reservationid, ehr.day, h.name AS hotelname, 
    ehr.totalrooms as requested_rooms, r.name AS roomname, 
    egh.bookedrooms
FROM event_hotel_reservation ehr 
INNER JOIN hotel_room_type r ON ehr.roomtypeid = r.roomtypeid 
INNER JOIN hotel h ON ehr.hotelid = h.hotelid    
left outer join (
    select hotelid, count(roomtypeid) as bookedrooms, day 
    from event_guest_hotel 
    where roomtypeid = 1 
    group by hotelid, day
) egh on h.hotelid = egh.hotelid and ehr.day = egh.day
WHERE totalRooms != 0 
    AND reservationID = '1'
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜