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
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?
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:
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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.