一尘不染

SQLite和自定义顺序

mysql

我有一张带有类别的表:

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

现在,我想将select语句的结果排序为

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

例如。在MySQL中,您可以这样做:

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

但是,您将如何在SQLite中做到这一点?我没有“订购依据”字段。


阅读 424

收藏
2020-05-17

共1个答案

一尘不染

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
2020-05-17