I have a query like this
SELECT *
FROM test JOIN test2 ON test.id=test2.id
WHERE test.id IN (562,553,572)
GROUP BY test.id
Its results are ordered like this: 553, 562, 572...
But I need the same order that I specified in the IN(562,553,572)
condition.
You can do this using FIELD()
:
SELECT ... ORDER BY FIELD(`test`.`id`, 562, 553, 572)
ORDER BY CASE test.id
WHEN 562 THEN 0
WHEN 553 THEN 1
WHEN 572 THEN 2
END
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