繁体   English   中英

mysql join查询从两个表中选择行,一个表有多个与第一个表匹配的行

[英]mysql join query for select rows from two tables that one table is having multiple rows matching to the first table

在此处输入图片说明

现在要在图像中获取结果,我正在使用此查询

select h.*, group_concat(distinct room_type) as RoomTypes
from hotel h 
left join rooms r on h.hotel_id = r.hotel_id
group by h.hotel_id;

但是此查询仅返回已归档的房间类型。 但我需要该表中的所有列。

请帮我。

尝试以下方法。 它假定可能有一个带wifi的房间,但没有早餐和所有其他组合。

SELECT h.*, SUM(r,availability) + ' rooms ' + 
            CASE WHEN r.wifi = 1 THEN ' with wifi ' ELSE '' END +
            CASE WHEN r.breakfast = 1 THEN ' with breakfast' ELSE '' END
            as RoomInfo
FROM hotel h 
LEFT JOIN rooms r ON h.hotel_id = r.hotel_id
GROUP BY h.hotel_id, r.room_type, r.wify, r.breakfast
HAVING SUM(r.availability) > 0;

好的格式将需要由前端处理。

尝试执行SQL

select h.*, CONCAT_WS(' ', r.availability, room_type, 'rooms', IF(r.wifi = 1 AND r.breakfast = 1,'with wifi and breakfast',IF(r.wifi = 1 ,'with wifi','with breakfast'))) AS Attributes
from hotels as h
inner join rooms as r on r.hotel_id = h.id
group by h.id, r.room_type, r.wifi, r.breakfast HAVING SUM(r.availability) > 0;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM