Warm tip: This article is reproduced from stackoverflow.com, please click
sql sql-server-2008 tsql

Sql Query help to get non matching records from two tables

发布于 2020-03-29 20:58:48

I am trying to get non matching records from 2 tables

For ex

TableA
 ID           Account
 1               Acc1
 2               Acc2
 3               Acc3

 TableB
 Opp          Accountid
 Opp1            1
 Opp2            2
 Opp3            4

I need to know which accountid which is present in TableB but not available in TableA. It would be wonderful someone could explain how you would approach this query.

Required record would be Opp3 of tableB

Thanks

Prady

Questioner
Prady
Viewed
33
Nighil 2011-04-30 15:32
create table #one (id int,acc nvarchar(25))
insert into #one (id , acc) values(1,'one') 
insert into #one (id , acc) values(2,'two') 
insert into #one (id , acc) values(3,'three') 

create table #two (acct nvarchar(25),ids int)
insert into #two (acct,ids) values('one',1) 
insert into #two (acct,ids) values('two',3) 
insert into #two (acct,ids) values('four',4) 

select ids from #two EXCEPT select id from #one 

drop table #one 
drop table #two 

test this one