从多个联接中获取数据

时间:2013-11-25 05:32:59

标签: mysql left-join

我有五张表有关系,如

Rooms
> Id | Hotel_ID | Room_Type
Room Price
> Id | Room_Id | Price | Start_Date
Room Image
> Id | Room_Id | Image
Room Amenity
> Id | Room_Id | Amenity
Amenity
> id | Amenity

在这种情况下,ROOM桌可以为酒店提供多个房间。对于每个房间,它可以有多个房间价格,房间图像,便利设施

我想找到一家酒店的所有房间,包括所有的价格,图像和舒适性。

我正在使用这个产生模糊结果的查询,即单行的多行。

SELECT hr.*, 
       group_concat(Distinct  ra.amenity_id) AS amenity_id,
       group_concat( Distinct  a.amenity) AS amenity,
       group_concat(Distinct  g.image) AS image,
       group_concat(Distinct rp.start_date) As Start_date
FROM hotel_rooms AS hr
LEFT JOIN hotel_room_amenity AS ra ON ra.room_id = hr.id
LEFT JOIN amenities AS a ON ra.amenity_id = a.id
LEFT JOIN hotel_room_gallery AS g ON hr.id = g.hotel_room_id
LEFT JOIN hotel_room_price AS rp ON hr.id = rp.room_id
WHERE hr.hotel_id = '24'
group by hr.id

0 个答案:

没有答案