GROUP_CONCAT DISTINCT BY ID

Nhật Anh

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.

Nhật Anh

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.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

mysql GROUP_CONCAT DISTINCT multiple columns

From Dev

MySQL GROUP_CONCAT(DISTINCT ...) ignores data

From Dev

mysql GROUP_CONCAT DISTINCT on joined rows

From Dev

Can we make a DISTINCT of a group_concat(distinct somefield)?

From Dev

how check id is in the group_concat field?

From Dev

how check id is in the group_concat field?

From Dev

Syntax to run a distinct GROUP_CONCAT in Google Bigquery

From Dev

Using R to COUNT() and GROUP_CONCAT(DISTINCT x)

From Dev

Mysql group_concat with distinct and where gives strange results

From Dev

Syntax to run a distinct GROUP_CONCAT in Google Bigquery

From Dev

How to get distinct values on GROUP_CONCAT using Google Big Query

From Dev

MySQL - GROUP_CONCAT returns duplicate data, can't use DISTINCT

From Dev

MySQL - GROUP_CONCAT returns duplicate data, can't use DISTINCT

From Dev

Why does GROUP_CONCAT + DISTINCT triggers a Out of memory (Needed 8388580 bytes) on some servers?

From Dev

MongoDb Aggregate $group '_id' null values as distinct

From Dev

how do i group rows by an id row in group_concat into one row string?

From Dev

Pandas group by unique ID and Distinct date per unique ID

From Dev

How to get distinct values from an array in mongoDB and group them by ID

From Dev

GROUP_CONCAT with limit

From Dev

Sorting in group_concat

From Dev

Group_concat use?

From Dev

example GROUP_CONCAT

From Dev

GROUP_CONCAT numbering

From Dev

GROUP_CONCAT in SQLite

From Dev

Group_concat use?

From Dev

example GROUP_CONCAT

From Dev

Group_Concat in Crosstab

From Dev

Group_Concat with duplicates

From Dev

MySQL Group_Concat Not In