我有这样的数据:
| hotel_id | city_name_en | country_name_en | city_name_ar | country_name_ar |
|----------|--------------|----------------------|--------------|-----------------|
| 4020874 | Dubai | United Arab Emirates | XXXX | XXXXXXXX |
| 4020882 | Dubai | United Arab Emirates | XXXX | XXXXXXXX |
| 4020890 | Dubai | United Arab Emirates | XXXX | XXXXXXXX |
| 4000053 | Al Fujayrah | United Arab Emirates | YYYY | YYYYYYYY |
| 4000058 | Al Fujayrah | United Arab Emirates | YYYY | YYYYYYYY |
| 4000060 | Al Fujayrah | United Arab Emirates | YYYY | YYYYYYYY |
我想要一个像这样的输出:
| hotel_id_list | name_en | name_ar |
|---------------------------|-----------------------------------|--------------------|
| 4020874, 4020882, 4020890 | Dubai, United Arab Emirates | XXXX, XXXXXXXXXXXX |
| 4000053, 4000058, 4000060 | Al Fujayrah, United Arab Emirates | YYYY, YYYYYYYYYYYY |
我想连接酒店ID并按city_name_en和city_name_ar对它们进行分组。
这是我迄今为止尝试的查询。
select
GROUP_CONCAT(DISTINCT hotel_id) AS 'hotel_id_list'
from (
select g1.hotel_id, g1.country_code, g1.city_name as city_name_en, g1.country_name as country_name_en, g2.city_name as city_name_ar, g2.country_name as country_name_ar
from general_details g1
inner join general_details g2 on g1.hotel_id = g2.hotel_id and g1.language_id=0 and g2.language_id=1) as t
where country_code = 'AE'
我可以连接酒店ids,但我也希望连接城市和国家。
答案 0 :(得分:0)
这样的事情应该有效:
SELECT GROUP_CONCAT(`hotel_id`) AS `hotel_id_list`
CONCAT(`city_name_en`, ', ', `country_name_en`) AS `name_en`
CONCAT(`city_name_ar`, ', ', `country_name_ar`) AS `name_ar`
FROM `<yourtablename>`
GROUP BY `city_name_en`, `country_name_en`, `city_name_ar`, `country_name_ar`