订购案例陈述的小节

时间:2016-08-09 21:26:52

标签: sql sqlite select sql-order-by

所以我有一个具有以下排序顺序的查询:

String sortOrder = "CASE " + ICCGoal.COLUMNS.category
        + " WHEN '" + GoalCategory.PHYSICAL_ACTIVITY.getEncodedValue() + "' THEN 1"
        + " WHEN '" + GoalCategory.SLEEP.getEncodedValue() + "' THEN 2"
        + " WHEN '" + GoalCategory.SOCIAL.getEncodedValue() + "' THEN 3"
        + " WHEN '" + GoalCategory.MOOD.getEncodedValue() + "' THEN 4"
        + " WHEN '" + GoalCategory.ENERGY.getEncodedValue() + "' THEN 5"
        + " END";

无论如何都要订购返回值的部分吗?

目前,我的所有目标都是" SLEEP"类型返回" PHYSICAL_ACTIVITY"之前"社交"目标,无论以何种顺序存储。

是否有可能按照创建日期的顺序返回它们?或者只是以默认的其他方式订购?

1 个答案:

答案 0 :(得分:3)

只需添加二级排序标准:

String sortOrder = "CASE " + ICCGoal.COLUMNS.category
        + " WHEN '" + GoalCategory.PHYSICAL_ACTIVITY.getEncodedValue() + "' THEN 1"
        + " WHEN '" + GoalCategory.SLEEP.getEncodedValue() + "' THEN 2"
        + " WHEN '" + GoalCategory.SOCIAL.getEncodedValue() + "' THEN 3"
        + " WHEN '" + GoalCategory.MOOD.getEncodedValue() + "' THEN 4"
        + " WHEN '" + GoalCategory.ENERGY.getEncodedValue() + "' THEN 5"
        + " END" // Original ordering from the OP
        + ", date_created"; // Secondary term