Here is the simple query with IN clause. But the problem is i need to get the output in the same order of the ids.
SELECT GROUP_CONCAT(username) as users FROM usertable WHERE usr_id IN (54,68,46)
For example if i pass 54,68,46 then the row with usr_id 54 should come first then 68 and then 46 should come. Is there any way to achieve this in MySQL?
We can use FIND_IN_SET
on order by clause to get the values in same order like this.
SELECT `username` as users FROM usertable WHERE usr_id IN (54,68,46) ORDER BY FIND_IN_SET(`usr_id`,"54,68,46")
But i don't know how to GROUP_CONCAT
in the same order. If anybody gives the answer for that in these kind of simple approach, i can accept that answer.
You need to use ORDER BY
clause in GROUP_CONCAT:
SELECT GROUP_CONCAT(username ORDER BY user_id ASC) as users
FROM usertable
WHERE usr_id IN (54,68);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With