如何订购与WHERE条件中指定的ID相同的查询结果? [英] How can I order a query result same as the id specified in the WHERE condition?
本文介绍了如何订购与WHERE条件中指定的ID相同的查询结果?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有这样的查询
SELECT *
FROM test JOIN test2 ON test.id=test2.id
WHERE test.id IN (562,553,572)
GROUP BY test.id
其结果按以下顺序排序:553、562、572 ...
Its results are ordered like this: 553, 562, 572...
但是我需要在IN(562,553,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)
这篇关于如何订购与WHERE条件中指定的ID相同的查询结果?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文