2

I need to use group_concat to build a list of comma separated values but I need the values to be quoted inside single quote. How to do this? The query which I have written doesn't work for me. I have values inside column like this:

userid (column)

 1)   1,2
 2)   3,4

Query 1:

SELECT GROUP_CONCAT( DISTINCT CONCAT('\'', user_id, '\'') ) as listed_id

Query 2:

SELECT GROUP_CONCAT( DISTINCT CONCAT('''', user_id, '''') ) as listed_id

Expected output:

'1','2','3','4'

But I am getting values like this

'1,2,3,4'
Dharman
  • 30,962
  • 25
  • 85
  • 135
Anonymous
  • 1,074
  • 3
  • 13
  • 37

1 Answers1

3

Try this, Its is working perfectly in my case:

SELECT GROUP_CONCAT( DISTINCT CONCAT("'", REPLACE(user_id, "," , "','") , "'")) as listed_id FROM users

Here is the output: enter image description here

  • My pleasure brother – Abdulrehman Sheikh Mar 29 '19 at 06:37
  • Neat workaround. But just to clarify for @Anonymous: `distinct` might not work as you expect (or maybe it does): if you have `1,2`, and `1,3` in your original table, you will still get `'1','2','1','3'`. `dictinct` will only refer to the complete entry (e.g. if you have `1,2` and `1,2`, you will get `'1','2'` only once). – Solarflare Mar 29 '19 at 06:41