mysql - 想用sql语句查询平均值,为何不是平均值呢?
PHPz
PHPz 2017-04-17 15:04:36
0
3
691

employee表的全部数据:

SELECT EMPLOYEE_id,salary,avg(salary) FROM test1.employees
group by employee_id
having avg(salary)>8000;

使用这个查询语句想得到平均工资大于8000的平均工资,可是为什么结果不是平均工资呢?

PHPz
PHPz

学习是最好的投资!

reply all(3)
小葫芦

Because you group by id, doesn’t there only be one salary under each ID?

select EMPLOYEE_id,salary,(select avg(salary) FROM employees)  from employees where salary > (select avg(salary) FROM employees);

Probably, you can give it a try

刘奇

You are grouping by employee_id. There is only one record for each employee_id in the table record. Doesn’t your avg have only one value?
If you want the average salary of all employees, don’t groupb by

迷茫

I have a problem understanding, average salary, what is average? If it is for employee_id, how can a person's salary be said to be average salary. Generally speaking, it is the salary of employees in a certain department whose average salary is greater than 8,000

select t.salary,t.employee_id,t1.avg_salary from employees t inner join (select department_id,avg(salary) avg_salary from employees group by department_id) t1 on t.department_id = t1.department and t1.avg_salary > 8000

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template