MySQL按请求顺序排序

时间:2014-05-25 14:16:30

标签: mysql sql sql-order-by

我想知道如何根据首先要求的方式执行请求。

示例:

SELECT name from user WHERE (guid = '6131bb732f6eec946e2121d1dc616019' OR guid = '62fdb8ecb0d5b7444f55aeba23fd606c' OR guid = 'f19fd7262163573abcf585d94fa74857')

我希望结果不是按id或其他任何方式排序,而是使用6131 *结果,然后是62f *然后是f19fd *

有办法吗?

1 个答案:

答案 0 :(得分:2)

SELECT name 
from user 
WHERE guid = '6131bb732f6eec946e2121d1dc616019' 
OR guid = '62fdb8ecb0d5b7444f55aeba23fd606c' 
OR guid = 'f19fd7262163573abcf585d94fa74857'
order by case when guid = '6131bb732f6eec946e2121d1dc616019' then 1
              when guid = '62fdb8ecb0d5b7444f55aeba23fd606c' then 2
              when guid = 'f19fd7262163573abcf585d94fa74857' then 3
         end

SELECT name 
from user 
WHERE guid in ('6131bb732f6eec946e2121d1dc616019', '62fdb8ecb0d5b7444f55aeba23fd606c', 'f19fd7262163573abcf585d94fa74857')
order by field(guid, '6131bb732f6eec946e2121d1dc616019', '62fdb8ecb0d5b7444f55aeba23fd606c', 'f19fd7262163573abcf585d94fa74857')