MySQL - WHERE子句,不减少行集

时间:2017-06-09 13:10:26

标签: mysql sql where

问题是并非所有的销售员都被包含在内,因为WHERE子句减少了行集......

  SELECT c.id, 
         c.name, 
         COUNT(o.id) AS count, 
         o.id, 
         o.date
    FROM clerks c JOIN 
         orders o ON c.id = o.id
   WHERE o.date BETWEEN $date AND $date2
GROUP BY c.id

有250名职员,但这只返回约50,因为WHERE子句减少了行集。如果他们不符合WHERE子句,是否有一些MySQL Magic包含一个Count为0的职员?

我试图避免子查询,因为我尝试的每一个都是如此慢。

1 个答案:

答案 0 :(得分:1)

SELECT c.id, 
         c.name, 
         COUNT(o.id) AS count, 
         o.id, 
         o.date
    FROM clerks c LEFT JOIN 
         orders o ON c.id = o.id and o.date BETWEEN $date AND $date2
GROUP BY c.id