MySQL YEARWEEK

SELECT YEARWEEK(‘2006-11-01’);

这是MySQL 5实力养成暨评量里的一个题目‘YEARWEEK 函数会对一个传入的日期,计算该日在当年的第几个星期,连同年份一起传回,试问执行附图中的SQL语法后结果为何?

答案:A

[adsense][/adsense]

MySQL 5.6 Reference Manual :: 12 Functions and Operators :: 12.7 Date and Time Functions这一节里有说明

YEARWEEK() : Return the year and week

YEARWEEK(date), YEARWEEK(date,mode)

Returns year and week for a date. The mode argument works exactly like the mode argument to WEEK(). The year in the result may be different from the year in the date argument for the first and the last week of the year.

mysql> SELECT YEARWEEK(‘1987-01-01’);
-> 198653
Note that the week number is different from what the WEEK() function would return (0) for optional arguments 0 or 1, as WEEK() then returns the week in the context of the given year.

在中译手册MySQL 5.1参考手册 :: 12.函数和操作符→12.5. 日期和时间函数

YEARWEEK( date ), YEARWEEK( date , start )

返回一个日期对应的年或周。start参数的工作同 start参数对WEEK()的工作相同。结果中的年份可以和该年的第一周和最后一周对应的日期参数有所不同。

mysql> SELECT YEARWEEK(‘1987-01-01’);

-> 198653

注意,周数和WEEK()函数队可选参数0或 1可能会返回的(0) w有所不同,原因是此时 WEEK()返回给定年份的语境中的周。

这里的重点应该在显示的格式以及每年的第一天是算在哪一周?