2

是否可以使用 SQL 语句从日期名称中提取整数值,即“Mon”、“Tue”、“Wed”?

例如:

 Mon = 1
 Tue = 2
 Wed = 3
4

4 回答 4

9

尝试场:

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

于 2010-05-08T07:37:47.407 回答
5

如果您只是有一个只有 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 |
+-----------+
于 2010-05-08T07:34:44.207 回答
1
DAYOFWEEK(date);

http://www.tutorialspoint.com/mysql/mysql-date-time-functions.htm#function_dayofweek

于 2011-04-13T14:15:31.847 回答
1

如果您的字段是日期字段,只需使用

SELECT DATE_FORMAT(my.field, "%w");

更多关于 MySQLs DATE_FORMAT 的信息可以在这里选择。

于 2010-05-08T07:39:04.450 回答