如何计算派生表中的行数?

时间:2014-06-12 17:49:07

标签: mysql sql database

我有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));

但是我收到了一个错误:

  

每个派生表必须具有别名“

对q结果中的行进行计数的正确查询是什么?

3 个答案:

答案 0 :(得分:3)

使用ALIAS;

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

答案 1 :(得分:1)

@ vjhil的答案是正确的,但我相信您的查询可以大大简化为:

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

或者进一步:

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

答案 2 :(得分:0)

在子查询前面添加一个别名并尝试执行。

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