2014-02-24 84 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'