MySQL按给定顺序选择WHERE

问题描述:

我有类似的东西

SELECT * FROM table WHERE id IN (118,17,113,23,72);

如果我只是这样做,它将以ID升序返回行.有没有一种方法可以按照IN语句中给出的顺序取回行?

If I just do this it returns the rows in ID ascending order. Is there a way to get back the rows in the order given in the IN statement?

您应使用"ORDER BY FIELD".因此,例如:

You should use "ORDER BY FIELD". So, for instance:

SELECT * FROM table WHERE id IN (118,17,113,23,72) 
ORDER BY FIELD(id,118,17,113,23,72)