我正在使用mysql的week()函数来确定日期的周数。问题是我面临的是它总是从实际周数返回少一个数字。假设今天是2013-05-15,因为它是今年的第20周,但结果是19.以下是我正在使用的查询。
SELECT *,WEEK(date_visit) AS week_no FROM property_view_details;
答案 0 :(得分:12)
答案 1 :(得分:7)
将第二个week
参数设置为正确值。
引用文档: https://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_week
M F.day Range Week 1 is the first week …
0 Sunday 0-53 with a Sunday in this year
1 Monday 0-53 with more than 3 days this year
2 Sunday 1-53 with a Sunday in this year
3 Monday 1-53 with more than 3 days this year
4 Sunday 0-53 with more than 3 days this year
5 Monday 0-53 with a Monday in this year
6 Sunday 1-53 with more than 3 days this year
7 Monday 1-53 with a Monday in this year
M = Mode
F.day = First day of week
样品:
mysql> SELECT WEEK('2008-02-20',0);
-> 7
mysql> SELECT WEEK('2008-02-20',1);
-> 8
答案 2 :(得分:1)
manual应该对此有所了解。
default_week_format
可能设置为默认值0
,这意味着一周从零开始计算。尝试将其更改为1
,或者只是调整查询以考虑更改。
答案 3 :(得分:1)
只需一个简单的谷歌即可研究该功能的操作:http://www.w3resource.com/mysql/date-and-time-functions/mysql-week-function.php
简而言之,您可以为该功能指定操作模式。也就是说,本周哪一天开始,以及是否应该从0或1开始。
例如,SELECT WEEK(date_visit, 2)
将计算周日开始的周数,并从1开始计算。
祝你好运。