I've the following table:
table
+--------------------+-------+
| id | name | age |
+--------------------+-------+
| 1 | client1 | 10 |
| 2 | client2 | 20 |
| 3 | client3 | 30 |
| 4 | client4 | 40 |
+--------------------+-------+
I'm trying to run a quest that would return the id, age
of the first row and a comma delimited string of ages of all rows except the first.
So the output of the query should give:
4, 40, client4, "3,2,1"
I try to use GROUP_CONCAT
in the following way:
SELECT id, age, name, SUBSTRING(GROUP_CONCAT(id), POSITION("," IN GROUP_CONCAT(id ORDER BY id DESC))+1) as previous_ids
FROM table
ORDER BY id DESC;
Query results:
1, 1, client1, "3,2,1"
It completely ignores the outer ORDER BY
.
Any idea how to fix this?? or perhaps a different approach..
Thank you!