一条SQL语句研究

现有“select * from t where a in (5,3,2,1,8,9,30...)" 假设 a 是主键,in里面的参数是唯一的。现要求输出的结果集按照 in 提供的参数顺序排序。而不是按照a本身的排序规则排序?   。

另:如果不要求使用临时表或表变量,那么又有什么办法实现。?

临时表方案参卡:

create table #t(id int identity(1,1),a int)

insert into #t values(5)

insert into #t values(3)

insert into #t values(2)

insert into #t values(1)

insert into #t values(8)

insert into #t values(9)

insert into #t values(30)

select t.* from t,#t as t1 where t.a=t1.a order by t1.id

如果不用临时表:(该方法极度不推荐,N倍的扫描全表时间)
select * from t where a=5 union all
select * from t where a=3 union all
select * from t where a=2 union all
select * from t where a=1 union all
select * from t where a=8 union all
select * from t where a=9 union all
select * from t where a=30
 

 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值