我有一个疑问,
SELECT E.staffid AS staff
SUM(E.Frequency='Less') AS lessCount,
SUM(E.Frequency='More') AS moreCount,
FROM effort_frequency E
INNER JOIN ost_staff S ON S.staff_id = E.staffid
WHERE E.log_date BETWEEN '2012-05-01' AND '2012-05-23'
GROUP BY E.staffid
我得到下表,
+-------+-----------+-----------+
| Staff | lessCount | moreCount |
+-------+-----------+-----------+
| 1 | 2 | 3 |
| 3 | 1 | 4 |
| 5 | 2 | 3 |
+-------+-----------+-----------+
您可以看到结果中缺少2 和 4,对于给定的日期,表中不会有 staff_id 2 和 4 的行effort_frequency
,但我正在尝试加入ost_staff
表,这样我就可以获得类似下表中,ost_staff 将包含所有员工 ID,比如说 1-5。
+-------+-----------+-----------+
| Staff | lessCount | moreCount |
+-------+-----------+-----------+
| 1 | 2 | 3 |
| 2 | 0 | 0 |
| 3 | 1 | 4 |
| 4 | 0 | 0 |
| 5 | 2 | 3 |
+-------+-----------+-----------+
我尝试了外部连接,但它不起作用,我想我在某个地方犯了错误。