2014-02-24 62 views
0

减去我有两个查询替代在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行。行应包含细节不仅包括计数。 我已经试过不存在和不在。 与此在表的复合键已被使用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'); 

请帮助,如果任何人有任何想法,如何做到这一点。

+1

'选择从第一一内连接第二个B上a.pid = b.project_id和a.project_name = b.project_name其中a.entry_date = COUNT(*) '2014-01-01'' –

+0

它返回83行作为计数,如何实现其他23行差异 – ajitksharma

回答

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' 
) 
+0

select * from first where active ='1'returns 106 rows and select * from second where entry_date ='2014- 01-17'返回78行,所以差异是28,为什么上面的查询返回31行..它应该返回28? – ajitksharma

+0

@ajitksharma可能由重复或空数据引起。请检查您的数据。 –

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'