开发者

SQLite - return sorted list

I can use

SELECT * FROM table 
WHERE id IN (478,278,190,890,123)

to return a list of records.

How can I instruct SQLite to 开发者_运维知识库return the records sorted using the order as specified in the list?


There is no dedicated instruction to such thing. You have to find a solution for each case.

In your example, you can do it like this:

SELECT *
  FROM table
 WHERE id IN (478,278,190,890,123)
 ORDER BY CASE id WHEN 478 THEN 0
                  WHEN 278 THEN 1
                  WHEN 190 THEN 2
                  WHEN 890 THEN 3
                  WHEN 123 THEN 4
          END

But if you have a long list of ids, it will become difficult to maintain.


you can use order by the field name. In this I am assuming id select * from table where id in (478,278,190,890,123) order by id

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜