member employee table ----------------------- company | group | name ----------------- -------- Company 1 | A Team | Zhang San Company 1 | A Team | Li Si Company 1 | A Team | Wang Wu Company 1 | B Team | | Team A | Xiao Ming
1. Count how many employees are there in each company?
SELECT `company`, Count(*) FROM `member` GROUP BY `company` ;
The following results are obtained: company | Count ------------------------------ Company 1 | 4 --------- --------------------- Company 2 | 2
2. Count how many employees are in each team under each company?
SELECT `company`, Count(*) FROM `member` GROUP BY `company` , `group` ;
The following results are obtained: company | group | Count ------------------------------ company 1 | A team | 3 ---- -------------------------- Company 1 | Team B | 1 ----------------- ------------- Company 2 | Team A | 2
GROUP BY X : Put all records with the same value of the X field into one group.
GROUP BY X, Y : Put all records with the same X field value and Y field value into one group.