在MySQl中替代Minus

时间:2014-02-24 09:48:40

标签: mysql

我有两个问题

  1) select count(*) from first where active='1'

  2) select count(*) 
     from first a left outer join second b 
         on a.pid=b.project_id and a.project_name=b.project_name 
     where a.entry_date='2014-01-01'

首先我得到106而第二我得到86。 我想显示106-83 = 26,26行。行应包含详细信息不仅要计数。 我已经尝试过Not Exists而不是In。 这两个表中的复合键都使用了project_id和project_name

我试过这个,但找到了0行

 select a.project_id, a.project_name 
 from first a 
 where a.active='1' and 
 NOT Exists(
     select  b.project_id, b.project_name 
     from first a 
     left outer join second b on a.pid=b.project_id 
     and a.project_name=b.project_name 
     where a.entry_date='2014-01-16');

如果有任何人有任何想法,请帮助。如何做。

2 个答案:

答案 0 :(得分:1)

试试这个:

select * 
from first 
where active='1'
and (pid, project_name) NOT IN (
  select project_id, project_name 
  from second
  where entry_date='2014-01-01'
)

答案 1 :(得分:0)

select count(*)
from first a inner join second b
on a.pid=b.project_id and a.project_name=b.project_name 
where a.active='1' and a.entry_date<>'2014-01-01'