douji0073 2014-05-26 02:40
浏览 17
已采纳

MySQL + PHP:每周显示项目?

This has me a stummped...

If I have a this MySQL table:

UserId | Commission | Date Of Commission
   1   |  200.00    |   2014-02-12
   1   |  50.00     |   2014-04-01
   2   |  10.00     |   2014-04-05

and I would like to display the Total Commission for a specific user per week starting from his/her first record, and display 0 for that range if there's no record.

how would I go about it?

Sample Output

UserId |     Date Range      | Total Commission
   1   | 02/10/14 - 02/16/14 |     200.00
   1   | 02/17/14 - 02/23/14 |      0.00
  ...
   1   | 03/31/14 - 04/06/14 |     50.00

I'm not a seasoned coder so any help will be much appreciated.

Thanks!

Edit:

I have tried this:

SELECT IFNULL(SUM(Commisssion),0) Total ,DATE_SUB(`DateOfCommission`,INTERVAL 7 DAY) 
  AS RangStart,DATE_SUB(`DateOfCommission`,INTERVAL 1 DAY) AS RangeEnd 
FROM `comms` WHERE `UserId` = '$UserID' GROUP BY DATE(`DateOfCommission`) DESC

but it starts the week with whatever date the first record was entered..

  • 写回答

4条回答 默认 最新

  • dsdtszi0520538 2014-05-26 03:45
    关注

    This is very tricky to accomplish. Here is what I managed to do with small modifications it should work they way it needs to be. I have done it for userid = 1 and this could be done for other users as well.

    In the query I have 2 lines

     where a.Date BETWEEN (select min(date) from transactions where UserId = 1) AND NOW()
    

    and

      WHERE date BETWEEN (select min(date) from transactions where UserId = 1) AND NOW()
    

    The query will try to generate the list of dates using the min() date of transaction for the user till today. Instead of now() this could be used as max() date of transaction for the user as well.

    select 
    t1.date_range,
    coalesce(SUM(t1.Commission+t2.Commission), 0) AS Commission
    from
    (
      select 
      a.Date as date,
      concat(
        DATE_ADD(a.Date, INTERVAL(1-DAYOFWEEK(a.Date)) +1 DAY),
        ' - ',
        DATE_ADD(a.Date, INTERVAL(7- DAYOFWEEK(a.Date)) +1 DAY)
      ) as date_range,
      '0' as  Commission
      from (
        select curdate() - INTERVAL (a.a + (10 * b.a) + (100 * c.a)) DAY as date
        from (select 0 as a union all select 1 union all select 2 union all select 3 union all select 4 union all select 5 union all select 6 union all select 7 union all select 8 union all select 9) as a
        cross join (select 0 as a union all select 1 union all select 2 union all select 3 union all select 4 union all select 5 union all select 6 union all select 7 union all select 8 union all select 9) as b
        cross join (select 0 as a union all select 1 union all select 2 union all select 3 union all select 4 union all select 5 union all select 6 union all select 7 union all select 8 union all select 9) as c
      ) a
      where a.Date BETWEEN (select min(date) from transactions where UserId = 1) AND NOW()
    )t1
    left join
    (
      SELECT date ,
      coalesce(SUM(Commission), 0) AS Commission
      FROM transactions
      WHERE date BETWEEN (select min(date) from transactions where UserId = 1) AND NOW()
      AND UserId = 1
      GROUP BY date
    )t2
    on t2.date = t1.date
    group by t1.date_range
    order by t1.date_range asc
    

    DEMO

    展开全部

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部