0
我有以下表模式,我想要獲得每個類別的金額列的總和和相應類別中的僱員數。MySQL集合函數與分組和加入
表1:員工
id | name | category
1 | SC | G 1.2
2 | BK | G 2.2
3 | LM | G 2.2
表2:payroll_histories
id | employee_id | amount
1 | 1 | 1000
2 | 1 | 500
3 | 2 | 200
4 | 2 | 100
5 | 3 | 300
輸出表應該是:
category | total | count
G 1.2 | 1500 | 1
G 2.2 | 600 | 2
我曾嘗試下面的總結和分組,但此查詢我無法讓伯爵工作。
查詢
SELECT employee_id, category, SUM(amount) from payroll_histories,employees WHERE employees.id=payroll_histories.employee_id group by category;
我已經試過了COUNT(類別),但一個也不能正常工作。
很好的解釋,我沒有使用第一個,但第二個和預期一樣。你可以留意組合爆炸。任何好的資源解釋這一點? – fanbondi 2015-03-24 22:15:12