对于Left Join和Group By

时间:2017-07-20 16:20:07

标签: php mysql arrays count

我只是想让下面的代码识别计数为零并返回0。

设置日期变量和周数表

//set first of the year and today
  $year = date("Y");
  $start = "01/01/".$year;
  $today = date("Y-m-d");
  $first = $year."-01-01";
  $start = $year."-01-01";

  $date1 = new DateTime($first);
  $date2 = new DateTime($today);
  $interval = $date1->diff($date2);

  $weeks = floor(($interval->days) / 7);

  //create weeks table
    $sql = "DROP TEMPORARY TABLE IF EXISTS weekstbl" ;
    mysqli_query ($db, $sql ) or ( "Error " . mysqli_error () ) ;

    $weekstbl = "
      CREATE TEMPORARY TABLE weekstbl (
        `weekNo` int NOT NULL,
        `weekStart` Date,
        `weekEnd` Date,
        PRIMARY KEY(weekNo)
      )
    ";

   mysqli_query($db, $weekstbl) or die ("Sql error : ".mysqli_error());

  for($i = 1; $i <= $weeks; $i++){    
      $week = $date1->format("W");
      $date1->add(new DateInterval('P6D'));
      $date1->format('Y-m-d');
      $newDate1 = $date1->format('Y-m-d');

      $statement = $db->prepare("INSERT INTO weekstbl (weekNo,weekStart,weekEnd) VALUES (?,?,?)");
      $statement->bind_param('iss', $week,$start,$newDate1);
      $statement->execute();

      $date1->add(new DateInterval('P1D'));
      $start = $date1->format('Y-m-d');
  }

此代码输出当前年度到当天的所有周数,周的开始日期和当周的结束日期。看起来如下:

------------------------------------
| weekNo |  weekStart |  weekEnd   |
------------------------------------
|   52   | 2017-01-01 | 2017-01-07 |
|    1   | 2017-01-08 | 2017-01-14 |
|    2   | 2017-01-15 | 2017-01-21 |

这将持续到访问系统的当前日期。然后,这将用于与其他几个有条件的表连接。这是我期望被退回的,但在下面列出的所有试验中,我无法做到这一点。我在本年度有12个星期时运行了这个,这意味着名字后面应该有12个数字。

[["Ryan Balcom",3,30,3,1,10,0,1,2,5,1,3,3],["Jared Beckwith",40,8,13,8,13,7,5,3,11,5,3,1],["Jim Roberts",0,32,8,7,2,9,4,2,8,4,4,10],["Jim Kelly",44,24,12,14,14,16,10,25,12,8,7,6],["Josh Bell",34,10,10,5,9,8,5,23,7,6,6,14],["Rick Zuniga",0,0,0,0,0,3,1,1,0,0,0,0],["Mike Horton",37,20,7,10,16,5,6,4,5,3,5,1],["Paul Schilthuis",31,11,9,2,8,6,4,0,5,9,8,4],["TJ Sutton",1,10,0,0,0,0,0,0,0,0,0,0]]

试验1

    $assignments = "
SELECT COUNT(j.leadid) AS leadcount, u.username
  FROM weekstbl wk
  LEFT JOIN jobbooktbl j
    ON wk.weekNo=WEEK(j.leadcreated)
  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";
    $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
    }

这会输出上述内容,但不会包含0周:

[["Ryan Balcom",3,30,3,1,10,1,2,5,1,3,3],["Jared Beckwith",40,8,13,8,13,7,5,3,11,5,3,1],["Jim Roberts",32,8,7,2,9,4,2,8,4,4,10],["Jim Kelly",44,24,12,14,14,16,10,25,12,8,7,6],["Josh Bell",34,10,10,5,9,8,5,23,7,6,6,14],["Rick Zuniga",3,1,1],["Mike Horton",37,20,7,10,16,5,6,4,5,3,5,1],["Paul Schilthuis",31,11,9,2,8,6,4,5,9,8,4],["TJ Sutton",1,10]]

试验2

$assignments = "
  SELECT COUNT(*) AS leadcount, u.username
    FROM weekstbl wk
    LEFT OUTER JOIN jobbooktbl j
      ON (wk.weekNo=WEEK(j.leadcreated)) AND j.leadcreated >= '".$first."' AND j.leadcreated <= '".$today."' AND YEAR(j.leadcreated) = '".$year."'
    LEFT JOIN assignmentstbl a
      ON j.leadid=a.custid
    LEFT JOIN usertbl u
      ON a.userid=u.userid
    GROUP BY wk.weekNo, a.userid";

这输出以下内容...不确定这是用空名称输出的内容:

[[null,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1],["Ryan Balcom",3,30,3,1,10,1,2,5,1,3,3],["Jared Beckwith",40,8,13,8,13,7,5,3,11,5,3,1],["Jim Roberts",32,8,7,2,9,4,2,8,4,4,10],["Jim Kelly",44,24,12,14,14,16,10,25,12,8,7,6],["Josh Bell",34,10,10,5,9,8,5,23,7,6,6,14],["Rick Zuniga",3,1,1],["Mike Horton",37,20,7,10,16,5,6,4,5,3,5,1],["Paul Schilthuis",31,11,9,2,8,6,4,5,9,8,4],["TJ Sutton",1,10]]

使用这两种方法,我试图包括COALESCE和IFNULL,但都没有改变任何查询的结果:

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

我一直在努力解决这个问题一个月,我扔的东西似乎没有用。任何协助或指示将不胜感激!

2 个答案:

答案 0 :(得分:0)

您需要将主表中的列添加到您的组中,否则您将按照仅存在于其中一个左联接中的值进行分组。

SELECT 
    COUNT(j.leadid) AS leadcount, 
    IF(u.username IS NULL,'NOT FOUND',u.username) AS username
FROM weekstbl wk
LEFT JOIN jobbooktbl j
    ON wk.weekNo=WEEK(j.leadcreated)
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."') OR
        j.leadcreated IS NULL
  GROUP BY wk.weekno, WEEK(j.leadcreated), a.userid
  ORDER BY wk.weekStart

更好的where子句与weekstbl中的日期进行比较。

答案 1 :(得分:0)

WHERE中的条件是将第一个LEFT JOIN转变为内连接。所以,你似乎想要:

SELECT COUNT(j.leadid) AS leadcount, u.username
FROM weekstbl wk LEFT JOIN
     jobbooktbl j
     ON wk.weekNo = WEEK(j.leadcreated) AND
        j.leadcreated >= '".$first."' AND j.leadcreated <= '".$today."' AND
        YEAR(j.leadcreated) = '".$year."'LEFT JOIN
     assignmentstbl a
     ON j.leadid = a.custid LEFT JOIN
     usertbl u
     ON a.userid = u.userid
GROUP BY WEEK(j.leadcreated), a.userid;

但是,您按周汇总,但不包括SELECT中的一周。我怀疑你想要:

SELECT wk.weekNo, u.username, COUNT(j.leadid) AS leadcount
FROM weekstbl wk LEFT JOIN
     jobbooktbl j
     ON wk.weekNo = WEEK(j.leadcreated) AND
        j.leadcreated >= '".$first."' AND j.leadcreated <= '".$today."' AND
        YEAR(j.leadcreated) = '".$year."'LEFT JOIN
     assignmentstbl a
     ON j.leadid = a.custid LEFT JOIN
     usertbl u
     ON a.userid = u.userid
GROUP BY wk.weekNo, a.userid;

您不希望按WEEK(j.leadcreated)进行汇总,因为它可能是NULL