gpt4 book ai didi

MySQL - 错误 1054 (42S22) : Unknown column 'sal' in 'having clause'

转载 作者:行者123 更新时间:2023-11-29 06:12:00 25 4
gpt4 key购买 nike

我需要按部门统计工资高于平均水平的员 worker 数。

我的员工表:

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

复制代码

此查询抛出以下错误:

错误 1111 (HY000):组函数使用无效

-

从 emp 组中选择 deptno, count(*) by deptno where sal > avg(sal)

而这个使用了不正确的语法:

您的 SQL 语法有误;查看与您的 MySQL 服务器版本对应的手册,了解在第 1 行的“where sal > avg(sal)”附近使用的正确语法

-

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

插入脚本:

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 - 错误 1054 (42S22) : Unknown column 'sal' in 'having clause' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37777835/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com