2014-06-12 3 views
2

나는 파생 테이블의 행 수를 계산하는 방법은 무엇입니까?

emp (id , name) 
cust(id, name) 

지금 내가이 쿼리 포즈하고이 를 들어

q=select emp.id from emp,cust where emp.id not in (select id from cust); 

의 결과의 행 수를 원하는 두 테이블

있습니다

select count(*) 
from (select emp.id 
     from emp,cust 
     where emp.id not in (select id from cust)); 

하지만 난은 무엇입니까 오류 :

Every derived table must have alias name"

q의 결과에서 행을 계산하는 올바른 쿼리는 무엇입니까?

+0

이 중 하나가 도움이 되었다면 답을 표시 (투표 아래의 체크 박스)하십시오. 행운을 빕니다! – paqogomez

답변

3

앨리어스 사용;

select count(*) from 
    (
    select emp.id from emp where emp.id not in (select id from cust) 
    )A; 
+0

탁월한 답변이지만 서식 (대문자, 코드 맞춤)은 일부 작업을 사용할 수 있습니다. ;-) +1 – paqogomez

1

vjhil의 대답은 정확하지만 난에 쿼리가 크게 간소화 할 수 있다고 생각 @ :

select count(*) 
from 
    emp e 
    left join cust c on e.id = c.id 
where 
    c.id is null 
0

가 하위 쿼리 앞에 별칭을 넣어 :

select count(*) 
from 
    emp 
where 
    emp.id not in (select id from cust) 

또는 추가로 실행을 시도하십시오.

select count(*) from (select emp.id from emp,cust where emp.id not in (select id from cust)) a; 
관련 문제