想要根据星期几对记录进行排序?

时间:2015-10-19 12:22:05

标签: php mysql wordpress

我在数据库中有一个字段名称session_dayofweek,它存储日期名称,例如'Mon'。我想获取session_dayofweek排序的记录。从星期日开始,记录应该像Sun,Mon,Tue等一样排序。我在wordpress数据库上运行查询。

这是我的问题:

$wpdb->pre_assignments = $wpdb->prefix . 'tc_pre_assignments';
$get_session_name=$wpdb->get_results("SELECT $wpdb->pre_assignments.*,$wpdb->sessions.* FROM $wpdb->pre_assignments JOIN $wpdb->sessions on $wpdb->pre_assignments.session_id = $wpdb->sessions.session_id WHERE $wpdb->sessions.session_id='".$session_id."'");

请帮帮我。提前谢谢。

1 个答案:

答案 0 :(得分:1)

您可以按

使用条件订单
ORDER BY CASE
   WHEN session_dayofweek = 'Sun' THEN 1
   WHEN session_dayofweek = 'Mon' THEN 2
   WHEN session_dayofweek = 'Tue' THEN 3
   WHEN session_dayofweek = 'Wed' THEN 4
   WHEN session_dayofweek = 'Thu' THEN 5
   WHEN session_dayofweek = 'Fri' THEN 6
   WHEN session_dayofweek = 'Sat' THEN 7
   ELSE 8
END

如果你正在寻找一种模式排序(又名sun,mon,tue,wed,thu,fri,sat,sun,mon,tue等)你可以做这样的事情

ORDER BY assignment_id, FIELD(session_dayofweek,'Sun','Mon','Tue','Wed','Thu','Fri','Sat')

sample fiddle