2012-06-15 54 views
0

我有兩個表,如行,獲取這在表1,但不是在表2

CREATE TABLE [dbo].[entry] 
(
    [id] [int] NULL, 
    [service] [int] NULL, 
    [sub] [int] NULL 
) 

的值,

id  service sub 
1  1  0 
2  1  1 
3  1  2 

第二個表是:

CREATE TABLE [dbo].[service] 
(
    [service] [int] NULL, 
    [sub] [int] NULL 
) 

和其數值爲:

service sub 
1   0 
1   1 

entry表中有3行,而在service表中有兩行。我想這是不是在service表,但該行是在entry

感謝

Venkat

+1

你嘗試過什麼?你有搜索過嗎?這是這裏最常見的問題之一。 –

回答

0

另一種方式來解決這個問題

Select a.id,a.service,a.sub from @entry as a where not EXISTS 
(select b.* from @service as b where a.sub = b.sub and a.service=b.service) 
1

試試這個:

Select entry.id, temp.service, temp.sub from entry INNER JOIN 
(Select service, sub from entry 
Except 
Select service, sub from service) as temp ON entry.service = temp.service AND entry.sub = temp.sub 
0

代碼的另一種變化使用ex CEPT。

select id, service, sub 
from entry 
except 
select e.id, e.service, e.sub 
from service as s left join entry as e 
on s.service = e.service 
and s.sub = e.sub 
0
SELECT 
    entry.id 
    , entry.service 
    , entry.sub 
FROM 
    entry 
LEFT JOIN SERVICE ON entry.service = service.service AND entry.sub = service.sub 
WHERE 
    entry.service IS NULL 
    AND entry.sub IS NULL 
相關問題