5

I am trying to select all id into one column and delimit it with a comma ,

My DATA column:

+---+--------+--------------+
|id |somedata|someother data|
+---+--------+--------------+
|1  |data1   |other1        |
+---+--------+--------------+
|2  |data2   |other2        |
+---+--------+--------------+
|3  |data3   |other3        |
+---+--------+--------------+

The result I am trying to make:

+-------+
|id list|
+-------+
|1, 2, 3|
+-------+

The result should be a list of ID's in 1 column named 'id list'.

The question is, is this possible? and how? I tried searching for the keywords sql query select into list and other keywords but with no luck.


But this query is on a nested select.

1
  • A question like this has been asked and answered a thousand times before. Commented Jan 13, 2014 at 5:22

1 Answer 1

5

Use GROUP_CONCAT():

SELECT GROUP_CONCAT(id) AS idList FROM tableA
Sign up to request clarification or add additional context in comments.

2 Comments

I'm getting syntax error on INTO. (edit) oh, I changed into to AS and it works. thanks.
I got it. It seemed weird to have the into in there. but I'm getting only the last id, example 3 ids in there the result is 3,3,3 instead of 1,2,3.. And on another note that query is on a nested select.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.