2013-05-15 27 views
-1

我使用mysql的week()函数来确定从日期开始的周数。问题是我面对的是它总是从实际的周数中返回少一个数字。假设今天是2013-05-15,因为它是今年的第20周,但结果是19.以下是我正在使用的查询。mysql week()函数返回少于一个实际的周数

SELECT *,WEEK(date_visit) AS week_no FROM property_view_details; 
+0

那么你的问题是什么?给结果添加1似乎并不困难:) –

+0

不,我不想在结果中添加一个:)我只想得到20不是19 – Farhan

+0

[阅读文档并修复您的错误](http:// dev .mysql.com/DOC/refman/5.5/EN /日期和时间functions.html#function_week)。 – Jocelyn

回答

10

直起:

mysql> SELECT WEEK('2008-02-20',1); 
    -> 8 

您可以通过第二个参数设置为改变WEEK行为或分别针对0索引或1索引结果的01

+0

感谢它的工作 – Farhan

+2

参数'1'将使它保持零索引,但将星期计数更改为ISO标准,即星期一开始的星期,第一周是新年里超过三天的星期。 '2'相当于默认模式,只改变了零索引。 –

6

将第二个参数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 
1

manual应该说明这一点。

default_week_format可能设置为默认0,这意味着该周从零开始计数。尝试将其更改为1,或者只是调整查询以解释更改。

相关问题