工资表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)中。