问题描述
是否可以编写一个 SQL 查询来按从特定日期开始的星期几对数据集进行排序?
Is it possible to write a SQL query that sorts data set by day of week starting from a specific day?
例如,如果今天是星期四,则结果从 THU-FRI-SAT-...-MON-TUE-WED 排序.
如果今天是星期二,结果将按照 TUE-WED-THU-...-SAT-SUN-MON 排序.
For example, if today is Thursday, the results are sorted from THU-FRI-SAT-...-MON-TUE-WED.
If today is Tuesday, the results would be sorted from TUE-WED-THU-...-SAT-SUN-MON.
天数存储为整数.
推荐答案
假设您将星期几存储在字段 WD
中,其中值 1 表示 MON,2 表示 TUE 等和 SW
是一周的开始"索引(同样是 1:MON, 2:TUE,...)然后是类似
Assuming you have the day of the week stored into field WD
where value 1 means MON, 2 means TUE etc and SW
is the "start of the week" index (again 1:MON, 2:TUE,...) then something like
CASE WHEN WD < SW THEN WD + 7 ELSE WD END
应该给你一个值来订购.我不使用 sqlite,所以我不确定您是否可以将其直接放入 ORDER BY
中,或者您是否必须将其用作字段然后按该字段排序.
should give you a value to order by. I don't use sqlite so I'm not sure can you put it right into the ORDER BY
or do you have to use it as a field and then order by that field.
这篇关于可以创建一个按星期几排序的查询吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!