MySQL + PHP:每周显示项目?

时间:2014-05-26 10:40:54

标签: php mysql

这让我感到沮丧......

如果我有这个MySQL表:

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

我想从他/她的第一张唱片开始每周显示特定用户的总佣金,如果没有记录则显示该范围的0。

我该怎么办呢?

示例输出

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

我不是一个经验丰富的编码员,所以任何帮助都会受到高度赞赏。

谢谢!

编辑:

我试过这个:

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

但它会以第一条记录的输入日期开始。

4 个答案:

答案 0 :(得分:1)

这非常棘手。这是我设法做的小修改,它应该以它需要的方式工作。我已经为userid = 1做了这个,这也可以为其他用户完成。

在查询中我有2行

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

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

查询将尝试使用用户的事务的min()日期生成日期列表,直到今天。而不是now(),这也可以用作用户的max()事务日期。

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

答案 1 :(得分:0)

所以,这是一种你可以使用的算法:

$Result = select distinct userid from table(this will fetch all userids from table)
while(There are rows in $Result)
{
    $Userid = $Result['userid']
    $StartDateRes = mysql_query(select Date, WEEKOFYEAR(Date) as week from table where userid = Userid order by date asc limit 1)
    $StartDateRow = mysql_fetch_assoc($StartDateRes)
    $StartDate = $StartDateRes['Date']
    $StartWeekNumber = $StartDateRes['week']

    $EndDateRes = mysql_query(select Date, WEEKOFYEAR(Date) as week from table where userid = Userid order by date desc limit 1)
    $EndDateRow = mysql_fetch_assoc($EndDateRes)
    $EndDate = $EndDateRes['Date']
    $EndWeekNumber = $EndWeekRes['week']

    for($i=$StartWeekNumber; $i<=$EndWeekNumber; $i++)
    {
        $StartDateOfWeek = FuncToFindStartDateOfWeek($i)
        $EndDateOfWeek = FuncToFindEndDateOfWeek($i)

        $Result2 = mysql_query(select sum(commission) as sum from table where date between StartDateOfWeek and EndDateOfWeek group by userid)

        $Row2= mysql_fetch_assoc($Result2)

        $Sum = $Row2['sum']

        mysql_query("insert into OutputTable values($UserId, $StartDateOfWeek. '-'. $EndDateOfWeek ,$Sum");
    }
}

答案 2 :(得分:0)

我能想到的最简单的方法如下

第1步:获取第一条记录的日期

“SELECT dateofcommission FROM comissionstable WHERE id ='userid'ORDER BY dateofcommission ASC LIMIT 1”

以上查询将仅返回第一个佣金日期

步骤2:创建一个循环,该循环从您在步骤1中获得的日期开始,并继续循环,直到日期大于或等于今天的日期。使用PHP日期函数增加此日期。

日期('Y-m-d',strtotime($ dateofcommission。'+ 7天'));

第3步:在此循环中,您可以在开始日期和结束日期获得佣金。开始日期将是添加7天之前的日期,结束日期将是您添加7天之后的日期。

SELECT SUM(佣金)FROM Commissiontable WHERE dateofcommission&gt; = startingdate AND dateofcomission&lt;结束日期AND id ='userid'

上述逻辑应该有效。如果您最终遇到一些与此逻辑有关的问题,请随时在评论中发帖。我很乐意帮助

以下是另一种解决方案

function getStartAndEndDate($week, $year) {
      $time = strtotime("1 January $year", time());
      $day = date('w', $time);
      $time += ((7*$week)+1-$day)*24*3600;
      $return[0] = date('Y-n-j', $time);
      $time += 6*24*3600;
      $return[1] = date('Y-n-j', $time);
      return $return;
}


$query = mysqli_query($con, "SELECT userid, COALESCE( SUM( commission ) , 0 ) AS thecommission , YEARWEEK( doc ) AS TheWeek FROM commission GROUP BY userid, TheWeek ORDER BY userid, TheWeek");
while ($array = mysqli_fetch_array($query)) {
  $test = $array['TheWeek'];
  $store_array = getStartAndEndDate(substr($test,4,2), substr($test,0,4));
  echo $array['userid']."-".$array['thecommission']."-".$store_array[0]."/".$store_array[1]."<br>";

}

答案 3 :(得分:0)

SELECT UserId, COALESCE(SUM(Commission),0), YEARWEEK(DateOfCommission) AS TheWeek
GROUP BY UserId, TheWeek
ORDER BY UserId, TheWeek;

这不会打印出漂亮的日期范围,但是应该让你从一个只有SQL的方向开始,在这个方向上,总和按一周中的一周进行细分。我想你可以从这一点开始添加更好的年份/周列格式。 YEARWEEK()应该给你很快的结果。