douzhangjian1505 2016-06-07 14:54
浏览 10
已采纳

使用sql和php按周获取数据

I have a problem and I have no idea how to do that. Please help me with an idea. So I have a table : test, with columns :

id        begin                    end     
1       2016-06-06 15:30:30     2016-06-08 16:40:40

Now I need to get the count of id by week The result need to be : For example if this week is the week number 6 I need to get :

Week           count
 4               10
 5               10
 6               12
 7               19
 8               27

Thx four you help

  • 写回答

1条回答 默认 最新

  • duanfu7994 2016-06-07 15:06
    关注

    Well, what happens if the begin is week 4, and end is week 5? which week is calculated?

    Anyway, if I understood you, you want two previous weeks and 2 future weeks :

    SELECT s.* FROM (
        SELECT YEAR(`start`) as year_col,WEEK(`start`) as week_col,count(*)
        FROM YourTable t
        GROUP BY year_col,week_col) s
    WHERE s.week_col between WEEK(now())-2 and WEEK(now())+2
    

    I've also added year into the consideration, since week 4 from 2015 will be group together with week 4 from 2016. If its not possible, exclude it from the query.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 安装svn网络有问题怎么办
  • ¥15 Python爬取指定微博话题下的内容,保存为txt
  • ¥15 vue2登录调用后端接口如何实现
  • ¥65 永磁型步进电机PID算法
  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥15 latex怎么处理论文引理引用参考文献