sql=select * from [channel] where id in (16,3,17,18,73)怎样按括号里的数字顺序来排序

来源:百度知道 编辑:UC知道 时间:2024/06/02 12:47:12
我是从指定的表中选出固定ID的行数据,例如:我选出ID是16,3,17,18,73这几行,查询出的结果也是按照这个顺序排(即:ID是16的排在第一,ID是3的排在第二,依次类推...。说白了:就是按照我括号里的ID数字顺序来排,不是按ID数字的大小排)

用union效率比较慢,我多添加了一个字段可以达到你的效果,如果你不想把这个字段显示出来需要嵌套一个select 将需要的字段列出即可
-------------
select
case id when 16 then 1
when 3 then 2
when 17 then 3
when 18 then 4
when 73 then 5
end as ordid
,* from [channel] where id in (16,3,17,18,73) order by ordid asc

sql=
"select * from [channel] where id =16
union
select * from [channel] where id =3
union
select * from [channel] where id =17
union
select * from [channel] where id =18
union
select * from [channel] where id =73"

我曰,说我回答重复了。你用union all来代替union,union不对的还是会自动排序的
select * from [channel] where id =16 union all select * from [channel] where id =3 union all select * from [channel] where id =17 union all select * from [channel] where id =18 union all select * from [channel] where id =73