一句SQL问题

k0yang 2011-02-11 09:24:25
一张 很简单的表 test
id no

1 a11
2 b22
3 b33
4 b44

select * from test where id in( 2,1,3)
我想要搜索出来的结果能根据in里面的ID进行排序
既按照 id
2
1
3

知道可以通过以下实现
select * from test where id = 2
union all
select * from test where id = 1
union all
select * from test where id = 3

但跪求更简单高效的方法
...全文
127 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
k0yang 2011-02-12
  • 打赏
  • 举报
回复
在hibernate中如何实现这个业务需求呢
shanxmxj 2011-02-11
  • 打赏
  • 举报
回复
select decode(id,'2',1,'1',2,'3',3) as id,no
from test
where id in( 2,1,3)

香草依含 2011-02-11
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 horizonlyhw 的回复:]
SQL code

select *
from test
where id in( 2,1,3)
order by decode(id,'2',1,'1',2,'3',3)
[/Quote]
这个很好,学习了。
WAN518814 2011-02-11
  • 打赏
  • 举报
回复
聚集函数的灵活使用果然很重要
horizonlyhw 2011-02-11
  • 打赏
  • 举报
回复

select *
from test
where id in( 2,1,3)
order by decode(id,'2',1,'1',2,'3',3)
heardy 2011-02-11
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 shanxmxj 的回复:]
Java code
select decode(id,'2',1,'1',2,'3',3) as id,no
from test
where id in( 2,1,3)
[/Quote]

这个原本的id序号就被改变了吧?

67,541

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧