我有两个具有以下属性的表:
表:部门
部门
部门名称
部门电话
部门大楼
部门经理
表:员工
emp_nbr
emp_lname
emp_fname
emp_phone
emp_出生日期
emp_date_hired
emp_nbr_of_dependents
员工部门
dept_nbr = emp_dept
我需要:
每个部门
1.) 总数员工家属人数
2.) 平均数量的家属。 - 我猜AVG
3.) 总数员工数量 - 我猜是 count(*)
有人可以帮我解决这个问题吗?以下是我的代码
select DEPT_NAME, AVG(EMP_NBR_OF_DEPENDENTs), count(emp_fname) as Total_No_of_Employees from dept,employee where DEPT_NBR = EMP_DEPT group by DEPT_NAME;
第二部分:
包括员 worker 数少于 50 人的部门
select DEPT_NAME, AVG(EMP_NBR_OF_DEPENDENTs), count(emp_fname) as Total_No_of_Employees from dept,employee where DEPT_NBR in (select EMP_DEPT from employee where count(emp_fname)<50) group by DEPT_NAME ;
我尝试了上述方法并收到错误 1111
谢谢大家
最佳答案
SELECT dept_name,SUM(emp_nbr_of_dependents),AVG(emp_nbr_of_dependents),COUNT(emp_nbr)
FROM deptartment JOIN employee ON dept_nbr=emp_dept
GROUP BY dept_name
HAVING COUNT(emp_nbr)<50
关于MYSQL跨多个表选择+使用数学运算符 Average/Count,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20081074/