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
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments