SELECT YEARWEEK(‘2006-11-01’);
這是MySQL 5實力養成暨評量裡的一個題目『YEARWEEK 函數會對一個傳入的日期,計算該日在當年的第幾個星期,連同年份一起傳回,試問執行附圖中的SQL語法後結果為何?
答案:A
在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()返回給定年份的語境中的周。
這裡的重點應該在顯示的格式以及每年的第一天是算在哪一週?