Reputation: 155
How can I have concatenated names ordered by their sequence in the set (it is a variable where I have ids comma separated like this "4,3,2")? So far the names are being ordered by id "2,3,4". This is my subquery.
SELECT GROUP_CONCAT(t.name SEPARATOR ',') FROM table t WHERE find_in_set(t.id, "4,3,2") > 0
Upvotes: 2
Views: 505
Reputation: 64476
You can use order by
in group_concat
function to order the names as in same order you have ids in find_in_set
,find_in_set
returns the index of matched value like for id => 4 index will be one for id=> 3 index will be 2 so you can order your result by using the result of find_in_set
SELECT
GROUP_CONCAT(t.name ORDER BY FIND_IN_SET(id, "4,3,2") SEPARATOR ',' )
FROM t
WHERE FIND_IN_SET(t.id, "4,3,2") > 0
Upvotes: 2