工資表t_salary如下:
| id | month | name | salary |
| 1 | 201601 | Jim | 12 |
| 2 | 201601 | Bruce | 30 |
| 3 | 201601 | Peter | 23 |
| 1 | 201602 | Jim | 20 |
示例一:
select id,sum(salary) from t_salary group by id;
示例一是對的
示例二:
select id,name,sum(salary) from t_salary group by id;
示例一會報錯。
因為使用group by 時,select 涉及的列要么是分組的依據(本例:id),要么包含在聚合函數(本例:salary)中。
