2016-06-12 123 views
1

我需要计算薪水高于平均水平的部门的雇员人数。MySQL - 错误1054(42S22):'having clause'中的未知列'sal'

我的员工表:

+------+--------+-----------+------+------------+------+------+--------+ 
| eno | ename | job  | mgr | hiredate | sal | comm | deptno | 
+------+--------+-----------+------+------------+------+------+--------+ 
| 7369 | smith | clerk  | 7902 | 1980-12-17 | 800 | NULL |  20 | 
| 7499 | allen | salesman | 7698 | 1981-02-20 | 1600 | 300 |  30 | 
| 7521 | ward | salesman | 7698 | 1981-02-22 | 1250 | 500 |  30 | 
| 7566 | jones | manager | 7839 | 1981-04-02 | 2975 | NULL |  20 | 
| 7654 | martin | salesman | 7698 | 1981-10-28 | 1250 | 1400 |  30 | 
| 7698 | blake | manager | 7839 | 1981-05-01 | 2850 | NULL |  30 | 
| 7782 | clark | manager | 7839 | 1981-06-09 | 2450 | NULL |  10 | 
| 7788 | scott | analyst | 7566 | 1982-12-09 | 3000 | NULL |  20 | 
| 7839 | king | president | NULL | 1981-11-17 | 5000 | NULL |  10 | 
| 7844 | turner | salesman | 7698 | 1981-10-08 | 1500 | NULL |  30 | 
| 7876 | adams | clerk  | 7788 | 1983-01-12 | 1100 | NULL |  20 | 
| 7900 | james | clerk  | 7698 | 1981-12-03 | 950 | NULL |  30 | 
| 7902 | ford | analyst | 7566 | 1981-12-03 | 3000 | NULL |  20 | 
| 7934 | miller | clerk  | 7782 | 1982-01-23 | 1300 | NULL |  10 | 
+------+--------+-----------+------+------------+------+------+--------+ 

我试图做的查询是

select deptno, count(*) from emp group by deptno having sal > avg(sal) 

,这时候我得到的错误。

我想这也查询:

select deptno, count(*), avg(sal) from emp group by deptno 

这基本上是除了没有having子句中的相同,它按预期工作。

-

select deptno, count(*) from emp where sal > avg(sal) group by deptno

此查询引发以下错误:

ERROR 1111 (HY000): Invalid use of group function

-

select deptno, count(*) from emp group by deptno where sal > avg(sal)

这Ø NE使用不正确的语法:

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'where sal > avg(sal)' at line 1

-

为什么会出现这个错误?这将是执行此查询的正确方法?提前致谢。

插入脚本:

SET SQL_MODE="NO_AUTO_VALUE_ON_ZERO"; 
SET time_zone = "+00:00"; 

/*!40101 SET @[email protected]@CHARACTER_SET_CLIENT */; 
/*!40101 SET @[email protected]@CHARACTER_SET_RESULTS */; 
/*!40101 SET @[email protected]@COLLATION_CONNECTION */; 
/*!40101 SET NAMES utf8 */; 

CREATE TABLE IF NOT EXISTS `emp` (
    `eno` int(11) NOT NULL AUTO_INCREMENT, 
    `ename` varchar(30) DEFAULT NULL, 
    `job` varchar(30) DEFAULT NULL, 
    `mgr` int(11) DEFAULT NULL, 
    `hiredate` date DEFAULT NULL, 
    `sal` int(11) DEFAULT NULL, 
    `comm` int(11) DEFAULT NULL, 
    `deptno` int(11) DEFAULT NULL, 
    PRIMARY KEY (`eno`) 
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=10000 ; 

INSERT INTO `emp` (`eno`, `ename`, `job`, `mgr`, `hiredate`, `sal`, `comm`, `deptno`) VALUES 
(7369, 'smith', 'clerk', 7902, '1980-12-17', 800, NULL, 20), 
(7499, 'allen', 'salesman', 7698, '1981-02-20', 1600, 300, 30), 
(7521, 'ward', 'salesman', 7698, '1981-02-22', 1250, 500, 30), 
(7566, 'jones', 'manager', 7839, '1981-04-02', 2975, NULL, 20), 
(7654, 'martin', 'salesman', 7698, '1981-10-28', 1250, 1400, 30), 
(7698, 'blake', 'manager', 7839, '1981-05-01', 2850, NULL, 30), 
(7782, 'clark', 'manager', 7839, '1981-06-09', 2450, NULL, 10), 
(7788, 'scott', 'analyst', 7566, '1982-12-09', 3000, NULL, 20), 
(7839, 'king', 'president', NULL, '1981-11-17', 5000, NULL, 10), 
(7844, 'turner', 'salesman', 7698, '1981-10-08', 1500, NULL, 30), 
(7876, 'adams', 'clerk', 7788, '1983-01-12', 1100, NULL, 20), 
(7900, 'james', 'clerk', 7698, '1981-12-03', 950, NULL, 30), 
(7902, 'ford', 'analyst', 7566, '1981-12-03', 3000, NULL, 20), 
(7934, 'miller', 'clerk', 7782, '1982-01-23', 1300, NULL, 10); 

/*!40101 SET [email protected]_CHARACTER_SET_CLIENT */; 
/*!40101 SET [email protected]_CHARACTER_SET_RESULTS */; 
/*!40101 SET [email protected]_COLLATION_CONNECTION */; 
+0

什么是你的MySQL版本? – Jocelyn

+0

https://dev.mysql.com/doc/refman/5.7/en/group-by-handling.html看看 – Naruto

+0

@Jocelyn我正在使用MySQL 5.5.24。 – SoKeT

回答

1

尝试用表

select emp.depnto, count(*) from 
(select deptno, avg(sal) avgsal from employees group by deptno) avg 
INNER JOIN employees emp on avg.deptno = avg.depnto 
WHERE emp.sal > avg.avgsal 

编辑

也许我thoght去繁就加入总和。当总体平均值意味着它只是

select depnto, count(*) from employees 
where sal > (select avg(sal) from employees) 
group by deptno 
+0

你的第二个解决方案是一个我一直在寻找,但我仍然不知道为什么它不能使用“where sal> avg(sal)”,并且它使用另一个查询。如果你能清楚这一点,我将不胜感激。尽管感谢解决方案。 – SoKeT

+0

在哪里你不能使用平均值,因为单行上有范围,稍后聚合就可以工作。在Having子句中,范围是组,所以您只能访问表列和分组列中的聚合函数。因此,如果您希望不在行范围内的值(having子句中的组)考虑它,则必须使用子选择:aggragation的vlaue决定要聚合的行,它如何工作? – Turo

+0

你说得对,我从来没有这样想过。非常感谢您的洞察力,这非常有帮助。再次感谢解决方案。 – SoKeT

0

你有一个逗号之前从

select deptno, count(*) 
from emp 
group by deptno having sal > (select avg(sal)from emp); 
+0

这是Stack Overflow的打字错误,我的不好;该查询仍然无效。感谢您的注意。 – SoKeT

+0

我已经更新了答案-.-一个建议..为什么downvote .. ansewer是正确的 – scaisEdge

+0

我没有downvote,我不知道为什么有人会。对于那个很抱歉。 – SoKeT

1

没有必要使用具有。尝试WHERE代替:

select deptno, count(*) from emp WHERE sal > avg(sal) group by deptno 
+0

不起作用,我认为where子句必须在组之前。您的查询会引发此错误:'您的SQL语法中有错误;检查与您的MySQL服务器版本相对应的手册,以便在第1行'where sal> avg(sal)'附近使用正确的语法。 – SoKeT

+0

我认为如果你能解释一下关于何时使用'Having'反对'Where'的话,它会改进你的答案? – Martin

+0

OH对不起 选择deptno,从emp emp计数(*)WHERE sal> avg(sal)group by deptno –

0

试试这个

SELECT deptno as Department , count(eno) as employeeCount from emp 
where sal > (select avg(sal) from emp) group by deptno 
+0

'ERROR 1242(21000):子查询返回多个1个row' – SoKeT

相关问题