1

大家好,我有以下与以下问题相关的问题

From tables emp and dept find number of employees working in every departments by their jobs.
Showing number not more than 5.

我写了下面的东西

select count(*)
from emp  e,dept d
where  e.department_id=d.department_id  and  count(*)<5

group by   e.department_id,e.job;

但它向我显示了以下错误

Error at Command Line:3 Column:23
Error report:
SQL Error: ORA-00933: SQL command not properly ended
00933. 00000 -  "SQL command not properly ended"
*Cause:    
*Action:

什么问题?请帮帮我

4

3 回答 3

4

尝试使用HAVING子句

SELECT  COUNT(*)
FROM    emp  e 
        INNER JOIN dept d 
            ON e.department_id=d.department_id  
GROUP   BY   e.department_id,e.job
HAVING  count(*)<5 ;
于 2013-02-04T14:48:14.513 回答
3

由于您尝试根据count(*)值进行过滤,因此需要使用HAVING子句。HAVING子句使用聚合函数进行过滤。

虽然@JW 的答案没有任何问题,但我会稍微更改它以使用 ANSIJOIN语法而不是子句中的JOIN语法:WHERE

select count(*) Total
from emp e
inner join dept d
    on e.department_id=d.department_id  
group by e.department_id, e.job 
having count(*) < 5

或者,您可以使您的查询成为子查询,以使用以下方式进行过滤WHERE

select Total
from
(
   select count(*) Total
   from emp e
   inner join dept d
      on e.department_id=d.department_id  
   group by e.department_id, e.job 
) src
where total < 5
于 2013-02-04T14:52:00.587 回答
2

尝试这个::having

select count(*)
from emp  e,dept d
where  e.department_id=d.department_id  

group by   e.department_id,e.job having count(*)<5;
于 2013-02-04T14:51:31.883 回答