简体   繁体   中英

MySQL query with avg and count greater than a value

I have table tbl_emply containing fields Salary , DNO , and EID for each employ. I need to find average Salary of each DNO that has more than two employees.

表内容

I have tried queries like

  1.  select avg(salary),DNO from tbl_emply where count(select * from tbl_emply group by(DNO)>2); 
  2.  select avg(salary),DNO from tbl_emply group by(DNO); 

But these all gave me invalid use of group by. How to get the result?

Use HAVING

SELECT AVG(salary), DNO
FROM tbl_emply
GROUP BY DNO
HAVING COUNT(*) > 2

试试这个,

select avg(Salary),DNo from tbl_emply group by DNo having count(*)>2;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM