我需要獲取給定日期的一週的開始日期和結束日期。 我需要給定日期的一週在星期一開始。MySQL WEEK(date [,mode]);工作還是我在這裏做錯了什麼?
如果我用out模式選項運行此查詢。
SELECT EXTRACT(YEAR FROM '2015-01-20') YEAR,
WEEK('2015-01-20') WK_No ,
STR_TO_DATE(CONCAT(EXTRACT(YEAR FROM '2015-01-20'), WEEK('2015-01-20'), 'Monday'), '%X%V %W') Mon,
STR_TO_DATE(CONCAT(EXTRACT(YEAR FROM '2015-01-20'), WEEK('2015-01-20'), 'Tuesday'), '%X%V %W') Tues,
STR_TO_DATE(CONCAT(EXTRACT(YEAR FROM '2015-01-20'), WEEK('2015-01-20'), 'Wednesday'), '%X%V %W') Wed,
STR_TO_DATE(CONCAT(EXTRACT(YEAR FROM '2015-01-20'), WEEK('2015-01-20'), 'Thursday'), '%X%V %W') Thur,
STR_TO_DATE(CONCAT(EXTRACT(YEAR FROM '2015-01-20'), WEEK('2015-01-20'), 'Friday'), '%X%V %W') Fri,
STR_TO_DATE(CONCAT(EXTRACT(YEAR FROM '2015-01-20'), WEEK('2015-01-20'), 'Saturday'), '%X%V %W') Sat,
STR_TO_DATE(CONCAT(EXTRACT(YEAR FROM '2015-01-20'), WEEK('2015-01-20'), 'Sunday'), '%X%V %W') Sun;
我得到以下結果
YEAR WK_No Mon Tues Wed Thur Fri Sat Sun
2015 3 2015-01-19 2015-01-20 2015-01-21 2015-01-22 2015-01-23 2015-01-24 2015-01-18
這一週在Sun 18
開始根據MySQL的幫助 http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_week
如果我將模式設置爲1,3 ,我本週5,7應該在星期一開始,但沒有一個做 如果我使用模式1或3,例如模式1
SELECT EXTRACT(YEAR FROM '2015-01-20') YEAR,
WEEK('2015-01-20',1) WK_No ,
STR_TO_DATE(CONCAT(EXTRACT(YEAR FROM '2015-01-20'), WEEK('2015-01-20',1), 'Monday'), '%X%V %W') Mon,
STR_TO_DATE(CONCAT(EXTRACT(YEAR FROM '2015-01-20'), WEEK('2015-01-20',1), 'Tuesday'), '%X%V %W') Tues,
STR_TO_DATE(CONCAT(EXTRACT(YEAR FROM '2015-01-20'), WEEK('2015-01-20',1), 'Wednesday'), '%X%V %W') Wed,
STR_TO_DATE(CONCAT(EXTRACT(YEAR FROM '2015-01-20'), WEEK('2015-01-20',1), 'Thursday'), '%X%V %W') Thur,
STR_TO_DATE(CONCAT(EXTRACT(YEAR FROM '2015-01-20'), WEEK('2015-01-20',1), 'Friday'), '%X%V %W') Fri,
STR_TO_DATE(CONCAT(EXTRACT(YEAR FROM '2015-01-20'), WEEK('2015-01-20',1), 'Saturday'), '%X%V %W') Sat,
STR_TO_DATE(CONCAT(EXTRACT(YEAR FROM '2015-01-20'), WEEK('2015-01-20',1), 'Sunday'), '%X%V %W') Sun;
我的結果是錯的只是沒有真正
YEAR WK_No Mon Tues Wed Thur Fri Sat Sun
2015 4 2015-01-26 2015-01-27 2015-01-28 2015-01-29 2015-01-30 2015-01-31 2015-01-25
我得到不包括日期和星期還有一週上週日
開始。如果我使用模式5或7例如模式5
SELECT EXTRACT(YEAR FROM '2015-01-20') YEAR,
WEEK('2015-01-20',5) WK_No ,
STR_TO_DATE(CONCAT(EXTRACT(YEAR FROM '2015-01-20'), WEEK('2015-01-20',5), 'Monday'), '%X%V %W') Mon,
STR_TO_DATE(CONCAT(EXTRACT(YEAR FROM '2015-01-20'), WEEK('2015-01-20',5), 'Tuesday'), '%X%V %W') Tues,
STR_TO_DATE(CONCAT(EXTRACT(YEAR FROM '2015-01-20'), WEEK('2015-01-20',5), 'Wednesday'), '%X%V %W') Wed,
STR_TO_DATE(CONCAT(EXTRACT(YEAR FROM '2015-01-20'), WEEK('2015-01-20',5), 'Thursday'), '%X%V %W') Thur,
STR_TO_DATE(CONCAT(EXTRACT(YEAR FROM '2015-01-20'), WEEK('2015-01-20',5), 'Friday'), '%X%V %W') Fri,
STR_TO_DATE(CONCAT(EXTRACT(YEAR FROM '2015-01-20'), WEEK('2015-01-20',5), 'Saturday'), '%X%V %W') Sat,
STR_TO_DATE(CONCAT(EXTRACT(YEAR FROM '2015-01-20'), WEEK('2015-01-20',5), 'Sunday'), '%X%V %W') Sun;
我得到與未使用模式選項相同的結果。
YEAR WK_No Mon Tues Wed Thur Fri Sat Sun
2015 3 2015-01-19 2015-01-20 2015-01-21 2015-01-22 2015-01-23 2015-01-24 2015-01-18
如何獲取給定日期的一週的開始日期和結束日期。 與給定日期的星期一開始?