当我使用IN从表中选择一组行时,例如
SELECT x.y, x.z FROM x WHERE x.id IN (23, 55, 44, 12)
是否有一个SQL技巧可以按照IN集中给出的顺序将它们恢复?
所以在这个例子中,假设x包含id为23,55,44和12的行,那么这四行将按此顺序返回。
答案 0 :(得分:12)
SELECT x.y, x.z FROM x WHERE x.id IN (23, 55, 44, 12)
ORDER BY FIELD (x.id, 23, 55, 44, 12)
答案 1 :(得分:4)
您可以将FIND_IN_SET用作:
SELECT x.y, x.z FROM x WHERE x.id IN (23, 55, 44, 12)
ORDER BY FIND_IN_SET(x.id,'23, 55, 44, 12');