sql=select * from [channel] where id in (16,3,17,18,73)怎样按括号里的数字顺序来排序
我是从指定的表中选出固定ID的行数据,例如:我选出ID是16,3,17,18,73这几行,查询出的结果也是按照这个顺序排(即:ID是16的排在第一,ID是3的排在第二,依...
我是从指定的表中选出固定ID的行数据,例如:我选出ID是16,3,17,18,73这几行,查询出的结果也是按照这个顺序排(即:ID是16的排在第一,ID是3的排在第二,依次类推...。说白了:就是按照我括号里的ID数字顺序来排,不是按ID数字的大小排)
展开
3个回答
展开全部
用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
-------------
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"
"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
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
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询