15

如何通过包含星期几名称的 varchar 列对 mysql 结果进行排序?

请注意,星期一应该先,而不是星期天。

4

10 回答 10

39

要么按照 Williham Totland 的建议重新设计列,要么进行一些字符串解析以获得日期表示。

如果该列包含星期几,那么您可以这样做:

ORDER BY FIELD(<fieldname>, 'MONDAY', 'TUESDAY', 'WEDNESDAY', 'THURSDAY', 'FRIDAY', 'SATURDAY', 'SUNDAY');
于 2009-07-14T17:57:22.997 回答
6

Why not this?

ORDER BY (
    CASE DAYOFWEEK(dateField)
    WHEN 1 THEN 7 ELSE DAYOFWEEK(dateField)
    END
)

I believe this orders Monday to Sunday...

于 2013-07-22T21:14:07.493 回答
3

我认为除了重新设计列以使用枚举来代替之外,没有太多工作要做,除了在你得到结果后对结果进行排序。

编辑:一个肮脏的黑客当然是添加另一个带有 id:weekday 对的表,并使用 joins 或 select in selects 来伪造一个枚举。

于 2009-07-14T17:55:16.073 回答
1

这看起来很乱,但仍然有效,而且看起来更通用:

select day, 
case day
  when 'monday' then 1
  when 'tuesday' then 2
  when 'wednesday' then 3
  when 'thursday' then 4
  when 'friday' then 5
  when 'saturday' then 6
  when 'sunday' then 7
end as day_nr from test order by day_nr;

使用 if 更加通用和混乱:

select id, day, 
if(day = 'monday',1,
  if(day = 'tuesday',2,
    if(day = 'wednesday',3,
      if(day = 'thursday',4,
        if(day = 'friday',5,
          if(day = 'saturday',6,7)
        )
      )
    )
  )
) as day_nr from test order by day_nr;

您还可以在存储过程中隐藏从 name 到 int 的转换细节。

于 2009-07-14T18:11:48.363 回答
1

... ORDER BY date_format(order_date, '%w') = 0, date_format(order_date, '%w') ;

于 2009-07-16T04:25:52.187 回答
1

I realise that this is an old thread, but as it comes to the top of google for certain search times I will use it to share my approach.

I wanted the same result as the original question, but in addition I wanted the ordering of the results starting from the current day of the week and then progressing through the rest of the days.

I created a separate table, in which the days were listed over a fortnight, so that no matter which day you started from you could run through a sequence of 7 days.

CREATE TABLE IF NOT EXISTS `Weekdays` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(50) NOT NULL DEFAULT '',
  PRIMARY KEY (`id`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=15 ;

INSERT INTO `Weekdays` (`id`, `name`) VALUES
(1, 'Monday'),
(2, 'Tuesday'),
(3, 'Wednesday'),
(4, 'Thursday'),
(5, 'Friday'),
(6, 'Saturday'),
(7, 'Sunday'),
(8, 'Monday'),
(9, 'Tuesday'),
(10, 'Wednesday'),
(11, 'Thursday'),
(12, 'Friday'),
(13, 'Saturday'),
(14, 'Sunday');

I then ran the query with a variable that determined the start point in sequence and used a join to get the order number for the days. For example to start the listing at Wednesday, I do the following:

SELECT @startnum := MIN(id) FROM Weekdays WHERE name='Wednesday';
SELECT * FROM Events INNER JOIN ( SELECT id as weekdaynum, name as dayname FROM Weekdays WHERE id>(@startnum-1) AND id<(@startnum+7) ) AS s2 ON s2.dayname=Events.day ORDER BY weekdaynum;

I hope this helps someone who stumbles onto this post.

于 2012-02-19T14:43:33.033 回答
1

Found another way, your can reverse order bye week

ORDER BY date_format(date_name, '%w') DESC;
于 2020-12-10T17:32:56.733 回答
0

另一种方法是创建另一个包含这些天的表和一个 int 来排序它们,在搜索时加入该表,然后按它排序。当然,不建议加入 varchar。

Table DaysOfWeek
id       | day
--------------------
1        | Monday
2        | Tuesday
3        | Wednesday
4        | Thursday
5        | Friday
6        | Saturday

SELECT * FROM WhatTable LEFT JOIN DaysOFWeek on DaysOFWeek.day = WhatTable.dayColumn ORDER BY DaysOfWeek.id

(抱歉,如果这不正确;我最近一直被 SQL Server 卡住)

同样,不建议这样做,但是如果您无法更改已经获得的数据...如果 dayColumn 字段中有非标准值,这也将起作用。

于 2009-07-14T18:00:41.367 回答
0

Found another way that works for me:

SELECT LAST_NAME, HIRE_DATE, TO_CHAR(HIRE_DATE, 'fmDAY') as 'Day' FROM EMPLOYEES
ORDER BY TO_CHAR(HIRE_DATE, 'd');    

Hope it helps

于 2017-10-11T14:37:49.477 回答
-1

If you try this, it should work:

SELECT ename, TO_CHAR(hiredate, 'fmDay') as "Day" 
FROM my_table
ORDER BY MOD(TO_CHAR(hiredate, 'D') + 5, 7)
于 2014-06-01T18:13:57.263 回答