MYSQL跨多个表选择+使用数学运算符 Average/Count

标签 mysql sql join

我有两个具有以下属性的表:

表:部门

部门

部门名称

部门电话

部门大楼

部门经理

表:员工

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/

相关文章:

Mysql从两个表中获取

SQL 查询 JOIN 或 IN 运算符?

mysql - 如何连接 2 个明细表 - MySql

mysql - 为什么一个查询中有 2 个左连接导致错误?

mysql - Doctrine2 多重连接

mysql - 我如何编写一个查询来显示拥有多个联系人的机构?

mysql - 从 MySQL 数据库中删除具有特定域的 URL

sql - 在列列表中使用子查询

MySQL 限制 MySQL 字段的值(允许一个特定值和多个其他值。)?

PHP fatal error : Call to a member function bind_param()