SQL WHERE IN(...)按列表顺序排序吗? [英] SQL WHERE IN (...) sort by order of the list?

查看:105
本文介绍了SQL WHERE IN(...)按列表顺序排序吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我已使用where子句查询数据库

Let's say I have query a database with a where clause

WHERE _id IN (5,6,424,2)

是否可以按照_id在列表中列出的顺序对返回的游标进行排序? _id属性在Cursor中从头到尾分别为5、6、424、2?

Is there any way for the returned cursor to be sorted in the order that the _id's where listed in the list? _id attribute from first to last in Cursor to be 5, 6, 424, 2?

这恰好是在Android上通过ContentProvider进行的,但这可能不相关.

This happens to be on Android through a ContentProvider, but that's probably not relevant.

推荐答案

使用子查询选择ID列表并加入它:

Select ID list using subquery and join with it:


select t1.*
from t1
inner join
(
  select 1 as id, 1 as num
  union all select 5, 2
  union all select 3, 3
) ids on t1.id = ids.id
order by ids.num

UPD:固定代码

这篇关于SQL WHERE IN(...)按列表顺序排序吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆