我需要按部门统计工资高于平均水平的雇员人数。
我的员工表:
+------+--------+-----------+------+------------+------+------+--------+
| 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 */;
最佳答案
尝试将聚合与表联接
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
关于mysql - MySQL-错误1054(42S22):“具有子句”中的未知列“sal”,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/37777835/