我有一個查詢,疑慮連接 - MySQL的
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在結果缺失,將會有沒有行 with 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 |
+-------+-----------+-----------+
我試過外連接,但它不工作,我想我在某個地方犯了錯誤。
這工作,我試圖,我得到了結果,非常感謝:) –