Kirtan
Kirtan

Reputation: 120

How to concatenate strings before GROUP_CONCAT

How do I prefix a string to a field before GROUP_CONCAT

this

id       test_id         
1           4              
2           4             
2           5            
3           5           
1           5
1           6

to

id       test_id         
1           id_4,id_5,id_6              
2           id_4,id_5             
3           id_5            

I want id_ prefixed to the test_id before I can get it by GROUP_CONCAT in MySQL

Upvotes: 2

Views: 2928

Answers (2)

xdazz
xdazz

Reputation: 160943

SELECT
  id,
  GROUP_CONCAT(CONCAT('id_', test_id))
FROM
  your_table
GROUP BY id

Upvotes: 9

Teneff
Teneff

Reputation: 32148

You need to use:

SELECT GROUP_CONCAT(CONCAT('id_', test_id)) FROM ....

Upvotes: 2

Related Questions