创建按周和用户分组的多维数组,同时显示零值

时间:2017-06-08 01:34:46

标签: php arrays multidimensional-array mysqli

这是我到目前为止所做的:

$year = date("Y");
$start = "01/01/".$year;
$today = date("Y-m-d");
$first = $year."-05-14";

$volumeYTDsm = [];

$assignments = "
SELECT COUNT(j.leadid) AS leadcount, DATE_ADD(j.leadcreated, INTERVAL(1-DAYOFWEEK(j.leadcreated)) DAY) AS weeks,
  DATE_ADD(j.leadcreated, INTERVAL(7-DAYOFWEEK(j.leadcreated)) DAY), u.username
  FROM jobbooktbl j
  INNER JOIN assignmentstbl a
    ON j.leadid=a.custid
  INNER JOIN usertbl u
    ON a.userid=u.userid
  WHERE j.leadcreated >= '".$first."' AND j.leadcreated <= '".$today."' AND YEAR(j.leadcreated) = '".$year."'
  GROUP BY WEEK(j.leadcreated), a.userid";
$assignmentsqry = mysqli_query($db,$assignments);
while ($row = mysqli_fetch_array($assignmentsqry)) {
  $sm = $row['username'];
  $float = floatval($row['leadcount']);
  $date = $row["weeks"];
  $newdate = date("n/j/Y", strtotime($date));
  array_push($volumeYTDsm, [$sm,$float]); 
}

这将输出以下多维数组:

[["Ryan Balcom",10],["Ryan Balcom",1],["Jared Beckwith",13],["Jared Beckwith",7],["Jared Beckwith",5],["Jared Beckwith",2],["Jim Roberts",9],["Jim Roberts",4],["Jim Roberts",2],["Jim Roberts",4],["Jim Kelly",14],["Jim Kelly",16],["Jim Kelly",10],["Jim Kelly",6],["Josh Bell",9],["Josh Bell",8],["Josh Bell",5],["Josh Bell",10],["Mike Horton",16],["Mike Horton",5],["Mike Horton",5],["Mike Horton",2],["Paul Schilthuis",7],["Paul Schilthuis",6],["Paul Schilthuis",4]]

这个结果有两个问题,我无法纠正。

1。如果计数为0,我需要计数返回0。 我试过以下无济于事:

COALESCE(COUNT(j.leadid),0) AS leadcount
IFNULL(COUNT(j.leadid),0) AS leadcount

2。我需要内部数组实际按用户分组,如下所示:

[["Ryan Balcom",0,0,10,1],["Jared Beckwith",13,7,5,2],["Jim Roberts",9,4,2,4],["Jim Kelly",14,16,10,6],["Josh Bell",9,8,5,10],["Mike Horton",16,5,5,2],["Paul Schilthuis",0,7,6,4]]

我仍在学习这些更复杂的查询。

更新

感谢Waldson Patricio,解决了一半难题。现在数组正确格式化,但它并没有像我希望的那样描绘0。

以下是更新后的代码:

$assignments = "
  SELECT COUNT(j.leadid) AS leadcount, DATE_ADD(j.leadcreated, INTERVAL(1-DAYOFWEEK(j.leadcreated)) DAY) AS weeks,
    DATE_ADD(j.leadcreated, INTERVAL(7-DAYOFWEEK(j.leadcreated)) DAY), u.username
  FROM jobbooktbl j
  LEFT JOIN assignmentstbl a
    ON j.leadid=a.custid
  LEFT JOIN usertbl u
    ON a.userid=u.userid
  WHERE j.leadcreated >= '".$first."' AND j.leadcreated <= '".$today."' AND YEAR(j.leadcreated) = '".$year."' AND u.salesman = 1
  GROUP BY WEEK(j.leadcreated), a.userid";
$assignmentsqry = mysqli_query($db,$assignments);
while ($row = mysqli_fetch_array($assignmentsqry)) {
  $key = $row['username']; //unnecessary variable for demonstration purposes
  if (!isset($volumeYTDsm[$key])) {
    $volumeYTDsm[$key] = [$row['username']];
  }
  $float = floatval($row['leadcount']);
    $volumeYTDsm[$key][] = $float;
  }
  $volumeYTDsm = array_values($volumeYTDsm);//removing keys
}

输出:

[["Ryan Balcom",10,1],["Jared Beckwith",13,7,5,2],["Jim Roberts",9,4,2,4],["Jim Kelly",14,16,10,6],["Josh Bell",9,8,5,10],["Mike Horton",16,5,5,2],["Paul Schilthuis",7,6,4]]

我已尝试将联接更改为以下但这也不起作用:

FROM usertbl u
LEFT JOIN assignmentstbl a
  ON u.userid=a.userid
LEFT JOIN jobbooktbl j
  ON a.custid=j.leadid

我也尝试过coalesce和ifnull而且没有:

COALESCE(COUNT(j.leadid),0) AS leadcount
IFNULL(COUNT(j.leadid),0) AS leadcount

1 个答案:

答案 0 :(得分:1)

回答你的问题:

  
      
  1. 如果计数为0
  2. ,我需要计数才能返回0   

您正在从jobbooktbl j中选择并在内部加入assignmentstbl a ON j.leadid=a.custid。此ON子句确保leadid必须等于a.custid。您的计数永远不会为0.如果您希望来自j的行与a表没有关系,请使用LEFT JOIN代替INNER JOIN(为此工作,你应该对其他INNER JOIN做同样的事情:

$assignments = "
SELECT COUNT(j.leadid) AS leadcount, DATE_ADD(j.leadcreated, INTERVAL(1-DAYOFWEEK(j.leadcreated)) DAY) AS weeks,
  DATE_ADD(j.leadcreated, INTERVAL(7-DAYOFWEEK(j.leadcreated)) DAY), u.username
  FROM jobbooktbl j
  LEFT JOIN assignmentstbl a
    ON j.leadid=a.custid
  LEFT JOIN usertbl u
    ON a.userid=u.userid
  WHERE j.leadcreated >= '".$first."' AND j.leadcreated <= '".$today."' AND YEAR(j.leadcreated) = '".$year."'
  GROUP BY WEEK(j.leadcreated), a.userid";
  
      
  1. 我需要内部数组实际按用户分组,如下所示:
  2.   

在此,我假设u.username是唯一的(如果它不是,您可以在字段列表中添加u.userid并将其用作密钥而不是用户名):

$assignmentsqry = mysqli_query($db,$assignments);
while ($row = mysqli_fetch_array($assignmentsqry)) {
    $key = $row['username']; //unnecessary variable for demonstration purposes

    if (!isset($volumeYTDsm[$key])) {
        $volumeYTDsm[$key] = [$row['username']];
    }

    $float = floatval($row['leadcount']);
    $volumeYTDsm[$key][] = $float;
    //$date = $row["weeks"]; // You're not using it
    //$newdate = date("n/j/Y", strtotime($date)); // You're not using it
}
$volumeYTDsm = array_values($volumeYTDsm);//removing keys