日期:2014-05-16  浏览次数:20742 次

mysql的查询语句的排序问题
SQL查询语句如下:
select * from table where (ID = 10) or (ID = 32) or (ID = 22) or (ID = 76) or (ID = 13) or (ID = 44)

我想让结果按10,32,22,76,13,44的顺序检索出来,但是检索后的顺序变成了按ID排序的:10,13,22,32,44,76。

请问如何让结果不排序呢???

多谢!

------解决方案--------------------


一个变通的方法

SQL code
select *,
case id 
    when 10 then 1 
    when 32 then 2 
    when 22 then 3 
    when 76 then 4 
    when 13 then 5 
    when 44 then 6 
END as seq
from table 
where (ID = 10) 
    or (ID = 32) 
    or (ID = 22) 
    or (ID = 76) 
    or (ID = 13) 
    or (ID = 44)
order by seq

------解决方案--------------------


SQL code
select *,
INSTR(',10,32,22,76,13,44,', ','+id+',') as seq
from table 
where id in(10,32,22,76,13,44)
order by seq

------解决方案--------------------
探讨
SQL codeselect *,
INSTR(',10,32,22,76,13,44,', ','+id+',') as seq
from table
where id in(10,32,22,76,13,44)
order by seq

== 思想重于技巧 ==

------解决方案--------------------
哈哈哈哈哈哈哈,你们都不行,看我的。


select *
from table 
where id in(10,32,22,76,13,44)
order by field (id,10,32,22,76,13,44);


------解决方案--------------------
select a.*, a.mark from 
(
select table.id, 600 as mark from table where table.id=10 union 
select table.id, 500 as mark from table where table.id=32 union 
select table.id, 400 as mark from table where table.id=22 union 
select table.id, 300 as mark from table where table.id=76 union 
select table.id, 200 as mark from table where table.id=13 union 
select table.id, 100 as mark from table where table.id=44 
) as a order by mark desc;