如何在MYSQL中按工作日三个字符排序

时间:2014-04-22 12:26:27

标签: mysql select

我有一个MYSQL表,其中包含工作日为三个字符的列,如

  星期一,星期二,星期三,星期四..

现在我想在这些工作日之前对 SELECT查询进行排序(不按字母顺序排列!)。当我使用以下行执行此操作时,它不起作用:

SELECT * FROM table ORDER BY STR_TO_DATE(Day, '%a')

你能给我一个有效的查询吗?

2 个答案:

答案 0 :(得分:2)

尝试以下代码

SELECT * FROM `table` ORDER BY FIELD(`day`, 'Mon', 'tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun');

答案 1 :(得分:0)

你可以尝试类似的东西(你没有提供有关表结构的更多信息,但无论如何):

SELECT DAYNAME(date) AS day, date, description
FROM table
ORDER BY DAYOFWEEK(date);

因此,您必须在表date中添加列,这是完整日期,以便排序生效。 它会产生类似的东西:

+----------+------------+-------------------------------------+
| day      | date       | description                         |
+----------+------------+-------------------------------------+
| Sunday   | 1809-02-12 | Lincoln's birthday                  |
| Sunday   | 1776-07-14 | Bastille Day                        |
| Monday   | 1215-06-15 | Signing of the Magna Carta          |
| Tuesday  | 1944-06-06 | D-Day at Normandy Beaches           |
| Thursday | 1989-11-09 | Opening of the Berlin Wall          |
| Friday   | 1732-02-22 | George Washington's birthday        |
| Friday   | 1958-01-31 | Explorer 1 launch date              |
| Friday   | 1957-10-04 | Sputnik launch date                 |
| Saturday | 1919-06-28 | Signing of the Treaty of Versailles |
| Saturday | 1789-07-04 | US Independence Day                 |
+----------+------------+-------------------------------------+
10 rows in set (0.00 sec)