Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
465 views
in Technique[技术] by (71.8m points)

sql - 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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...