Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I have a table with categories:

ID  Category
"1","Baking"   
"3","Family"   
"4","Entertaining"   
"5","Children"   
"6","Desserts"   

Now I would like to order the result of the select statement to

ID  Category
"4","Entertaining"   
"3","Family"  
"1","Baking"   
"5","Children"   
"6","Desserts"  

for example. In MySQL, you'd do it like this:

SELECT * FROM CATEGORIES ORDER BY FIELD (ID, 4,3,1,5,6);

How would you do it in SQLite though? I don't have an "order by" field.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
502 views
Welcome To Ask or Share your Answers For Others

1 Answer

ORDER BY 
  CASE ID
    WHEN 4 THEN 0
    WHEN 3 THEN 1
    WHEN 1 THEN 2
    WHEN 5 THEN 3
    WHEN 6 THEN 4
  END

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share

548k questions

547k answers

4 comments

86.3k users

...