自我加入MySql表

时间:2018-01-05 15:47:05

标签: php mysql join

所以我在我的数据库中有一些用户,并且每个用户都能够提交推荐信息'到我们公司。我正在创建一个表格,显示每个用户从当月和上个月提交的推荐数量,以便我们可以跟踪他们是否是活跃的销售代表。虽然我成功显示了给定用户的当前月份数字,但我一直很难正确显示上个月的信息。我得到了上个月推荐的数量,但它为每个用户提供了相同的数字。

|=========|========|==========|=============|
|user_id   refer_id  referals      date     |
|-------------------------------------------|
|1             1     mcdonalds   2017-12-19 |
|1             2     Burger King 2017-12-19 |
|1             3     Wendys      2017-12-21 |
|1             4     Arby's      2017-12-22 |
|1             5     In n' out   2018-01-02 |
|2             6     Chipotle    2018-01-03
|2             7     Carl's jr.  2018-01-04 |
|===========================================|

用户1应该有1个推荐,而用户2应该有2个当前月份的推荐,这确实有效,而用户1应该有4个推荐上个月,但是每个用户返回4个。我还想将其浓缩为一个查询,但我不确定使用什么类型的JOIN,或者我甚至需要一个?我已经找到了关于SELF JOINS和INNER JOIN以及之前有关它的问题的信息,但是我不确定在我的情况下使用哪个。

  <table class="table table-hover">
    <thead>
      <tr>
        <th>Sales Name</th>
        <th>Current Month</th>
        <th>Previous Month</th>
      </tr>
    </thead>

    <?php   
      //CURRENT MONTH QUERY 

      $query = "SELECT user_id, COUNT(refer_id) AS refer_total
                FROM table
                WHERE MONTH(date) = MONTH(CURDATE()) GROUP BY user_id";
      $result = mysqli_query($connection, $query); 

      //PREVIOUS MONTH QUERY   

     $query2 = "SELECT user_id, COUNT(refer_id) AS previous_total
                 FROM table
                 WHERE 
                 YEAR(date) = YEAR(CURRENT_DATE - INTERVAL 1 MONTH) AND
                 MONTH(date) = MONTH(CURRENT_DATE - INTERVAL 1 MONTH) GROUP BY user_id";     
     $total = mysqli_query($connection, $query2);          

     while($values = mysqli_fetch_assoc($total)) {    
       $previous = $values['previous_total'];
     }

     while($values = mysqli_fetch_assoc($result)) { 
       $user_id = $values['user_id'];
       $num_refer = $values['refer_total'];
       echo "<tr><td>$user_id</td><td>$num_refer</td><td>$previous</td></tr>";
     }

     if(mysqli_num_rows < 5) {
       echo "Inactive!";
     }
  ?>
</table> <!--END TABLE-->

1 个答案:

答案 0 :(得分:1)

您还需要使用日期功能按日期分组。显示当前月份在您的结果中是正确的唯一原因是因为每个用户当前月份为1。

mysql> select user_id, date_format(date,'%Y-%m'), count(*) from test group by user_id, date_format(date,'%Y-%m');
+---------+---------------------------+----------+
| user_id | date_format(date,'%Y-%m') | count(*) |
+---------+---------------------------+----------+
|       1 | 2017-12                   |        4 |
|       1 | 2018-01                   |        1 |
|       2 | 2018-01                   |        1 |
+---------+---------------------------+----------+
3 rows in set (0.00 sec)

实际上我刚才意识到这不是你想要的格式。以下内容应该有效:

mysql> select user_id, 
sum(case date_format(date,'%Y-%m') when date_format(curdate(),'%Y-%m') then 1 else 0 end) as current_month, 
sum(case date_format(date,'%Y-%m') when date_format(DATE_SUB(curdate(), INTERVAL 1 MONTH),'%Y-%m') then 1 else 0 end) as previous_month 
from test group by user_id;
+---------+---------------+----------------+
| user_id | current_month | previous_month |
+---------+---------------+----------------+
|       1 |             1 |              4 |
|       2 |             1 |              0 |
+---------+---------------+----------------+
2 rows in set (0.00 sec)