简体   繁体   English

MySQL连接多列的数据,跳过重复

[英]MySQL concatenate data from multiple columns skipping dups

I am looking for a way to group duplicate column data. 我正在寻找一种将重复的列数据分组的方法。
I have a table with data like: 我有一个数据表,如:

-------------------------------------------
ID  | locationId | day | open     | close
-------------------------------------------
1   | 1213       | Wed | 10:00 am | 4:30 pm
-------------------------------------------
2   | 1213       | Thr | 10:00 am | 4:30 pm
-------------------------------------------
3   | 1213       | Fri | 10:00 am | 4:30 pm
-------------------------------------------
4   | 1213       | Sat | 10:00 am | 5:30 pm
-------------------------------------------

I am using group_concat to return the information for locationId 1213 as 我正在使用group_concat返回locationId 1213的信息,如下所示:

Wed: 10:00 am-04:30 pm,Thu: 10:00 am-04:30 pm,Fri: 10:00 am-04:30 pm,Sat: 10:00 am-05:30 pm

Is there a way to use group_concat or another function/query to group the duplicate open and close times and return the data similar to the following? 有没有一种方法可以使用group_concat或其他函数/查询将重复的打开和关闭时间分组,并返回类似于以下内容的数据?

Wed,Thr,Fri: 10:00 am-04:30 pm,Sat: 10:00 am-05:30 pm

or even 甚至

Wed-Fri: 10:00 am-04:30 pm,Sat: 10:00 am-05:30 pm

Below is the SQL code that I am using: 以下是我正在使用的SQL代码:

SELECT appdata.nameShort, 
group_concat(distinct concat(appdatahours.`day`,": ", appdatahours.open, "-", 
appdatahours.close)) as hours
FROM appdata
LEFT JOIN appdatalocations on appdatalocations.listingId = appdata.id
LEFT JOIN appdatahours on appdatahours.locationId = appdatalocations.id
GROUP BY appdata.id

try this (uses a derived table): 试试看(使用派生表):

select locationId, group_concat(day_group) from
(
    SELECT
    locationID, concat(group_concat(`day`),": ", open, "-", close) as day_group
    FROM tableB
    GROUP BY locationID, open, close
) dayGroup;

sqlfiddle here: http://sqlfiddle.com/#!2/c19c5/1/0 sqlfiddle此处: http ://sqlfiddle.com/#!2/c19c5/1/0

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

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