2016-06-12 121 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

相關問題