MySQL - ERROR 1054(42S22):'having子句'中的未知列'sal'

时间:2016-06-12 18:44:11

标签: mysql sql select group-by having

我需要按部门计算员工人数,薪水高于平均水平。

我的员工表:

+------+--------+-----------+------+------------+------+------+--------+
| 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)

这个使用的语法不正确:

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 @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@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 CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;

4 个答案:

答案 0 :(得分:1)

没有必要使用HAVING。试试WHERE:

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

答案 1 :(得分: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

修改

也许我觉得很复杂。当总体平均值意味着它只是

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

答案 2 :(得分:0)

您之前有逗号

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

答案 3 :(得分:0)

试试这个

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