Sqlite,在不弄乱第一列的情况下对第二列进行排序
Sqlite, sort second column without messing the order in the first
我有以下 table:
15|flex |2|low |Not started
16|flex |1|medium|Not started
17|flex |2|medium|Not started
18|gridonly-med|1|low |Not started
19|gridonly-med|2|low |Not started
20|gridonly-med|4|low |Not started
21|gridonly-med|1|medium|Not started
22|gridonly-med|2|medium|Not started
23|gridonly-med|4|medium|Not started
我需要的是按第 3 列对它(desc)进行排序,而不会弄乱第 2 列和第 4 列。
基本上,我需要得到这个 table:
15|flex |2|low |Not started
17|flex |2|medium|Not started
16|flex |1|medium|Not started
20|gridonly-med|4|low |Not started
19|gridonly-med|2|low |Not started
18|gridonly-med|1|low |Not started
23|gridonly-med|4|medium|Not started
22|gridonly-med|2|medium|Not started
21|gridonly-med|1|medium|Not started
我试过:
ORDER BY column4 DESC, column1 ASC
但是没用。
我得到:
23|gridonly-med|4|medium|Not started
20|gridonly-med|4|low |Not started
22|gridonly-med|2|medium|Not started
19|gridonly-med|2|low |Not started
17|flex |2|medium|Not started
21|gridonly-med|1|medium|Not started
18|gridonly-med|1|low |Not started
16|flex |1|medium|Not started
如果有人指出我可以用来解决我的问题的任何条款或任何其他事情,我将不胜感激任何帮助,谢谢。
您必须先按 col2
排序,然后按 col4
排序,最后按 col3
降序排序:
ORDER BY col2, col4, col3 DESC
请注意,我使用 'flex'
在 'gridonly-med'
之前按字母顺序排序而 'low'
在 medium
之前按字母顺序排序的事实,因此如果有不同的值,代码不行。
对于某些结果,您应该使用条件排序:
ORDER BY col2 = 'flex' DESC,
col4 = 'low' DESC,
col3 DESC
或:
ORDER BY CASE col2 WHEN 'flex' THEN 1 ELSE 2 END,
CASE col4 WHEN 'low' THEN 1 ELSE 2 END,
col3 DESC
并且您可以扩展 CASE
表达式的分支以包含更多值。
见demo.
我有以下 table:
15|flex |2|low |Not started
16|flex |1|medium|Not started
17|flex |2|medium|Not started
18|gridonly-med|1|low |Not started
19|gridonly-med|2|low |Not started
20|gridonly-med|4|low |Not started
21|gridonly-med|1|medium|Not started
22|gridonly-med|2|medium|Not started
23|gridonly-med|4|medium|Not started
我需要的是按第 3 列对它(desc)进行排序,而不会弄乱第 2 列和第 4 列。
基本上,我需要得到这个 table:
15|flex |2|low |Not started
17|flex |2|medium|Not started
16|flex |1|medium|Not started
20|gridonly-med|4|low |Not started
19|gridonly-med|2|low |Not started
18|gridonly-med|1|low |Not started
23|gridonly-med|4|medium|Not started
22|gridonly-med|2|medium|Not started
21|gridonly-med|1|medium|Not started
我试过:
ORDER BY column4 DESC, column1 ASC
但是没用。
我得到:
23|gridonly-med|4|medium|Not started
20|gridonly-med|4|low |Not started
22|gridonly-med|2|medium|Not started
19|gridonly-med|2|low |Not started
17|flex |2|medium|Not started
21|gridonly-med|1|medium|Not started
18|gridonly-med|1|low |Not started
16|flex |1|medium|Not started
如果有人指出我可以用来解决我的问题的任何条款或任何其他事情,我将不胜感激任何帮助,谢谢。
您必须先按 col2
排序,然后按 col4
排序,最后按 col3
降序排序:
ORDER BY col2, col4, col3 DESC
请注意,我使用 'flex'
在 'gridonly-med'
之前按字母顺序排序而 'low'
在 medium
之前按字母顺序排序的事实,因此如果有不同的值,代码不行。
对于某些结果,您应该使用条件排序:
ORDER BY col2 = 'flex' DESC,
col4 = 'low' DESC,
col3 DESC
或:
ORDER BY CASE col2 WHEN 'flex' THEN 1 ELSE 2 END,
CASE col4 WHEN 'low' THEN 1 ELSE 2 END,
col3 DESC
并且您可以扩展 CASE
表达式的分支以包含更多值。
见demo.