sql语句能得到自然周的信息吗?

解决方案 »

  1.   

    +----+-------+---------------------+
    | id | name  | createTime          |
    +----+-------+---------------------+
    |  1 | test1 | 2012-08-02 00:00:00 |
    |  2 | test2 | 2012-08-01 00:00:00 |
    |  3 | test3 | 2012-08-01 00:00:00 |
    |  4 | test4 | 2012-07-01 00:00:00 |
    |  5 | test5 | 2012-07-02 00:00:00 |
    |  6 | test6 | 2012-07-03 00:00:00 |
    |  7 | test7 | 2012-07-15 00:00:00 |
    |  8 | test7 | 2012-07-23 00:00:00 |
    |  9 | test7 | 2012-07-21 00:00:00 |
    +----+-------+---------------------+
    根椐这些信息统计每周一到周日的信息个数  2012/6/25(周一) - 2012/7/1(周日)   有多少个
    2012/7/2(周一)  - 2012/7/8(周日)    有多少个
    2012/7/9(周一)  - 2012/7/15(周日)    有多少个
      

  2.   

    +----+-------+---------------------+-----+
    | id | name  | createTime          | num |
    +----+-------+---------------------+-----+
    |  1 | test1 | 2012-08-02 00:00:00 |   1 |
    |  2 | test2 | 2012-08-01 00:00:00 |   1 |
    |  3 | test3 | 2012-08-01 00:00:00 |   1 |
    |  4 | test4 | 2012-07-01 00:00:00 |   1 |
    |  5 | test5 | 2012-07-02 00:00:00 |   1 |
    |  6 | test6 | 2012-07-03 00:00:00 |   1 |
    |  7 | test7 | 2012-07-15 00:00:00 |   1 |
    |  8 | test7 | 2012-07-23 00:00:00 |   1 |
    |  9 | test7 | 2012-07-21 00:00:00 |   1 |
    +----+-------+---------------------+-----+
      

  3.   

    select week(createTime) ,count(*) from tt group by week(createTime) 
      

  4.   


    +----+-------+---------------------+-----+
    | id | name  | createTime          | num |
    +----+-------+---------------------+-----+
    |  1 | test1 | 2012-08-02 00:00:00 |   2 |
    |  2 | test2 | 2012-08-01 00:00:00 |   1 |
    |  3 | test3 | 2012-08-01 00:00:00 |   1 |
    |  8 | test7 | 2012-07-23 00:00:00 |   1 |
    |  9 | test7 | 2012-07-21 00:00:00 |   1 |
    |  7 | test7 | 2012-07-15 00:00:00 |   1 |
    |  6 | test6 | 2012-07-03 00:00:00 |   1 |
    |  5 | test5 | 2012-07-02 00:00:00 |   1 |
    |  4 | test4 | 2012-07-01 00:00:00 |   1 |
    +----+-------+---------------------+-----+我表述有点问题
    最后的结果应该是
    2012/6/25(周一) - 2012/7/1(周日)    1
    2012/7/2(周一)  - 2012/7/8(周日)    2
    2012/7/9(周一)  - 2012/7/15(周日)   1
    2012/7/16(周一)  - 2012/7/22(周日)  1
    2012/7/23(周一)  - 2012/7/29(周日)  1
    2012/7/30(周一)  - 2012/8/5(周日)   4每个自然的num字段求和   应该能得到6条记录才对呀
      

  5.   

    分组求和
    select week(createTime) ,sum(num) from tt group by week(createTime)
      

  6.   

    哦,为什么SQL 求出来是4条求和信息,应该是6条啊 ?求解答
      

  7.   

    是的,是 6 条,你的写法是 4 条
    select week(createTime, 1) ,sum(num) from tt group by week(createTime, 1)
    这样才是 6 条
    WEEK(date,first) 
    对于星期日是一周中的第一天的场合,如果函数只有一个参数调用,返回 date 为一年的第几周,返回值范围为 0 到 53 (是的,可能有第 53 周的开始)。两个参数形式的 WEEK() 允许你指定一周是否以星期日或星期一开始,以及返回值为 0-53 还是 1-52。 这里的一个表显示第二个参数是如何工作的: 值  含义  
    0  一周以星期日开始,返回值范围为 0-53  
    1  一周以星期一开始,返回值范围为 0-53  
    2  一周以星期日开始,返回值范围为 1-53  
    3  一周以星期一开始,返回值范围为 1-53 (ISO 8601)  
      

  8.   

    SELECT week( createTime, 1 ) , count( * ) 
    FROM `test` 
    GROUP BY week( createTime, 1 );
    WEEK(date,first)返回date是一年的第几周(first默认值0,first取值1表示周一是周的开始,0从周日开始)