我正在寻找一种对重复列数据进行分组的方法 我有一个包含以下数据的表:
-------------------------------------------
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
-------------------------------------------
我正在使用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
有没有办法使用group_concat或其他函数/查询对重复的打开和关闭时间进行分组并返回类似于以下内容的数据?
Wed,Thr,Fri: 10:00 am-04:30 pm,Sat: 10:00 am-05:30 pm
甚至
Wed-Fri: 10:00 am-04:30 pm,Sat: 10:00 am-05:30 pm
以下是我正在使用的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
答案 0 :(得分:1)
试试这个(使用派生表):
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:http://sqlfiddle.com/#!2/c19c5/1/0