I have 2 tables like this:
Table a:
ID type
1 1
1 1
2 1
2 2
2 2
3 2
Table b:
ID name
1 a
2 b
3 b
And expected result:
ID type name
1,2 1 a,b
2,3 2 b,b
I have tried:
SELECT GROUP_CONCAT(DISTINCT ID), a.type,
GROUP_CONCAT(SELECT name FROM b WHERE ID = a.ID)
FROM a GROUP BY a.type
but it's not working. I need name is GROUP_CONCAT and DISTINCT by ID.
I have found a solution for my question:
SELECT GROUP_CONCAT(DISTINCT a.id), a.type,
(SELECT GROUP_CONCAT(name) FROM b
WHERE FIND_IN_SET(ID, GROUP_CONCAT(a.ID)))
FROM a GROUP BY a.type
Try this one:
SELECT GROUP_CONCAT(DISTINCT a.id), a.type, GROUP_CONCAT(DISTINCT b.name)
FROM a
LEFT JOIN b ON a.id = b.id
GROUP BY a.type
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.