转自:http://bbs.csdn.net/topics/370096126
t_table有数据如下:
year Week
2011 2
2011 3
2011 4
2011 5
想知道2011年的第二周起始日期和结束日期是哪2个?
SELECT
year
, week, x.start, ADDDATE(x.start, 6)
AS
end
FROM
(
SELECT
ADDDATE(jan1, (t.week - WEEK(jan1, 5)) * 7 - WEEKDAY(jan1))
AS
start,
year
, week
FROM
(
SELECT
MAKEDATE(`
year
`, 1)
AS
jan1,
year
, week
FROM
t_table) t
) x;
+
------+------+------------+------------+
|
year
| week | start |
end
|
+
------+------+------------+------------+
| 2011 | 1 | 2011-01-03 | 2011-01-09 |
| 2011 | 2 | 2011-01-10 | 2011-01-16 |
| 2011 | 3 | 2011-01-17 | 2011-01-23 |
| 2011 | 52 | 2011-12-26 | 2012-01-01 |
+
------+------+------------+------------+
其中week(date, mode) 函数的含义:http://dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html#function_week