Jarede
Jarede

Reputation: 3488

mysql select distinct rows into a comma delimited list column

I currently have some sql that brings back tags. they should have distinct ids, but they don't.... so my current data is like:

Microsoft | GGG | 1 | 167
Microsoft | GGG | 1 | 2
Microsoft | GGG | 1 | 1

What i would like to do is have only one row come back with the final column concatenated into a delimited list like:

Microsoft | GGG | 1 | 167, 2, 1

I am using mySQL 5 for this.

Upvotes: 16

Views: 15267

Answers (1)

Michael Berkowski
Michael Berkowski

Reputation: 270617

Use GROUP_CONCAT() for this, with a GROUP BY covering the other three columns:

SELECT 
  name,   -- Microsoft
  other,  -- GGG
  other2, -- 1
  GROUP_CONCAT(id) AS ids
FROM tbl
GROUP BY name, other, other2

Upvotes: 36

Related Questions