按日期显示周

时间:2012-02-14 11:31:49

标签: mysql datetime date

以下是我的内容

 date
+++++++++++++
 2012-02-01
 2012-02-03
 2012-02-05
 2012-02-07
 2012-02-08
 2012-02-10
 2012-02-14
 2012-02-15
 2012-02-17
 2012-02-17
 2012-03-01
 2012-03-03
 2012-03-05
 2012-03-07

我想要的是如下

week| date
++++++++++++++++++
  1 | 2012-02-01
  1 | 2012-02-03
  1 | 2012-02-05
  1 | 2012-02-07
  2 | 2012-02-08
  2 | 2012-02-10
  2 | 2012-02-14
  3 | 2012-02-15
  3 | 2012-02-17
  3 | 2012-02-17
  4 | 2012-03-01
  4 | 2012-03-03
  4 | 2012-03-05
  4 | 2012-03-07

我试过

SELECT WEEKDAY(date) AS week, date
FROM myTable
WHERE date BETWEEN start_date AND end_date
ORDER BY week

以下就是我得到的

week| date
++++++++++++++++++
  1 | 2012-02-01
  1 | 2012-02-03
  1 | 2012-02-05
  1 | 2012-02-07
  1 | 2012-03-01
  1 | 2012-03-03
  1 | 2012-03-05
  1 | 2012-03-07
  2 | 2012-02-08
  2 | 2012-02-10
  2 | 2012-02-14
  3 | 2012-02-15
  3 | 2012-02-17
  3 | 2012-02-17

这是不正确的,因为2012-03-01以后显示为1应该是4 ...让我知道我错在哪里...

提前致谢!!!

此问题与this

有关

3 个答案:

答案 0 :(得分:2)

而不是使用WEEKDAY使用WEEKOFYEAR

SELECT WEEKOFYEAR(date) AS week, date
FROM myTable
WHERE date BETWEEN start_date AND end_date
ORDER BY week

<强>更新 This question is the source

SELECT  (WEEK(date, 5) -
        WEEK(DATE_SUB(date, INTERVAL DAYOFMONTH(date) - 1 DAY), 5) + 1) as week, date
    FROM myTable
    WHERE date BETWEEN start_date AND end_date
    ORDER BY week

更新2

SELECT  (WEEK(date, 1) -
        WEEK(DATE_SUB(date, INTERVAL DAYOFMONTH(date) - 1 DAY), 1) + 1) as week, date
    FROM myTable
    WHERE date BETWEEN start_date AND end_date
    ORDER BY week

答案 1 :(得分:0)

如果我运行此查询,我得到了这个结果。在前两天是相同的,但我得到不同的周数:(

这是我的结果:

WEIGHT | WEEK | CREATED_ON
60 | 2 | 2012-02-08 16:16:32
200 | 3 | 2012-02-08 16:16:30
63 | 4 | 2012-02-01 16:16:32
70 | 5 | 2012-01-25 16:16:32
75 | 6 | 2012-01-17 16:16:32
60 | 7 | 2012-01-08 16:16:32
63 | 8 | 2012-01-01 16:16:32
70 | 9 | 2011-12-25 16:16:32
75 | 10 | 2011-12-17 16:16:32
60 | 11 | 2011-12-08 16:16:32
63 | 12 | 2011-12-01 16:16:32
60 | 12 | 2011-12-01 16:16:32
70 | 14 | 2011-11-23 16:16:32
75 | 15 | 2011-11-17 16:16:32
63 | 16 | 2011-11-01 16:16:32
抱歉!我是新的堆栈流和mysql抱歉不方便stackOverFlow语法。

答案 2 :(得分:0)

我找到了解决方案......

下面会给我我想要的东西......

SELECT (SELECT COUNT(distinct u2.myDate) FROM myTable u2
WHERE 
u2.myDate > u1.myDate) + 1 AS week, myDate FROM myTable u1
WHERE myDate between startDate AND endDate
ordeR BY week

更新1:

以下是我的内容

myDate
+++++++++++++++++++++
2012-02-01 12:12:12
2012-02-01 12:12:12
2012-02-01 12:12:12
2012-02-03 12:12:12
2012-02-05 12:12:12
2012-02-07 12:12:12
2012-02-08 12:12:12
2012-02-08 12:12:12
2012-02-10 12:12:12
2012-02-14 12:12:12
2012-02-15 12:12:12
2012-02-17 12:12:12
2012-02-17 12:12:12
2012-03-01 12:12:12
2012-03-03 12:12:12
2012-03-05 12:12:12
2012-03-07 12:12:12

使用上面的查询,下面是我得到的......

week |myDate
+++++++++++++++++++++++++++
  1  |2012-03-07 12:12:12
  2  |2012-03-05 12:12:12
  3  |2012-03-03 12:12:12
  4  |2012-03-01 12:12:12
  5  |2012-02-17 12:12:12
  5  |2012-02-17 12:12:12
  6  |2012-02-15 12:12:12
  7  |2012-02-14 12:12:12
  8  |2012-02-10 12:12:12
  9  |2012-02-08 12:12:12
  9  |2012-02-08 12:12:12
  10 |2012-02-07 12:12:12
  11 |2012-02-05 12:12:12
  12 |2012-02-03 12:12:12
  13 |2012-02-01 12:12:12
  13 |2012-02-01 12:12:12
  13 |2012-02-01 12:12:12