是否可以使用 SQL 语句从日期名称中提取整数值,即“Mon”、“Tue”、“Wed”?
例如:
Mon = 1
Tue = 2
Wed = 3
尝试场:
SELECT FIELD('Mon', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun');
-> 1
SELECT FIELD('Thu', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun');
-> 4
http://dev.mysql.com/doc/refman/5.4/en/string-functions.html#function_field
如果您只是有一个只有 Mon、Tue、Wed 等的非日期时间字段,您可以使用STR_TO_DATE()
andWEEKDAY()
函数来提出如下内容:
SELECT WEEKDAY(STR_TO_DATE(CONCAT('201011 ', yourField), '%X%V %W')) + 1 AS WeekIndex;
测试用例:
SELECT WEEKDAY(STR_TO_DATE(CONCAT('201011 ', 'Mon'), '%X%V %W')) + 1 AS WeekIndex;
+-----------+
| WeekIndex |
+-----------+
| 1 |
+-----------+
SELECT WEEKDAY(STR_TO_DATE(CONCAT('201011 ', 'Tue'), '%X%V %W')) + 1 AS WeekIndex;
+-----------+
| WeekIndex |
+-----------+
| 2 |
+-----------+
SELECT WEEKDAY(STR_TO_DATE(CONCAT('201011 ', 'Wed'), '%X%V %W')) + 1 AS WeekIndex;
+-----------+
| WeekIndex |
+-----------+
| 3 |
+-----------+
SELECT WEEKDAY(STR_TO_DATE(CONCAT('201011 ', 'Thu'), '%X%V %W')) + 1 AS WeekIndex;
+-----------+
| WeekIndex |
+-----------+
| 4 |
+-----------+
DAYOFWEEK(date);
http://www.tutorialspoint.com/mysql/mysql-date-time-functions.htm#function_dayofweek