我有三个SQL表,我正在尝试将department id
,average employee age
,the range of employee salary(min-max)
和total number of employees
列表作为表格。
Emp ( eid : INTEGER, ename: STRING; age: INTEGER; salary : REAL )
Works ( eid: INTEGER, did: INTEGER, pct_time: INTEGER )
Dept ( did: INTEGER, budget: real, manager_id: INTEGER )
我正在使用左连接来查找每个部门的员工总数,如下所示并且有效。
SELECT d.did, COUNT(e.eid) AS total_employees
FROM works d LEFT JOIN works e ON e.did = d.did
GROUP BY d.did;
但是当我试图找到部门的最低工资时,MySQL会给出错误。
SELECT w.did, COUNT(e.eid) AS total_employees, e.salary
FROM works w LEFT JOIN emp e ON e.eid = w.eid
GROUP BY w.did;
ERROR 1055(42000):SELECT列表的表达式#3不在GROUP BY中 子句并包含非聚合列' sqlw1.e.salary'不是 功能上依赖于GROUP BY子句中的列;这是 与sql_mode = only_full_group_by mysql>不兼容SELECT w.did, COUNT(e.eid)AS total_employees,e.salary FROM工作w LEFT JOIN emp e ON e.eid = w.eid
有人可以告诉我这里试图获得薪水的错误是什么?另外,我很感激您对如何加入这三个表以获取列值的评论。
答案 0 :(得分:1)
在这种类型的查询中,您必须将列放入group by
,或者您必须在列上使用某些聚合函数。
您需要将查询更改为
SELECT w.did, COUNT(e.eid) AS total_employees, MIN(e.salary)
FROM works w LEFT JOIN emp e ON e.eid = w.eid
GROUP BY w.did;
我们使用MIN()
函数来获取最小e.salary
答案 1 :(得分:0)
您必须找到员工的MIN工资,因此您需要按员工ID分组,
尝试以下查询。
SELECT w.did, COUNT(e.eid) AS total_employees, MIN(e.salary) AS salary
FROM works w LEFT JOIN emp e ON e.eid = w.eid
GROUP BY w.eid;