| Author |
Topic |
|
rudba
Constraint Violating Yak Guru
415 Posts |
Posted - 2009-02-19 : 13:29:19
|
| I have two tables same same format.How do i list the record which is not exist on next table.On tbl1ID12345On tbl2Id1567Now i have to list the ID's from tbl2 which is not exist on tbl1 |
|
|
sodeep
Master Smack Fu Yak Hacker
7174 Posts |
Posted - 2009-02-19 : 13:33:19
|
| [code]Select ID from Table1exceptSelect Id from Table2[/code] |
 |
|
|
rudba
Constraint Violating Yak Guru
415 Posts |
Posted - 2009-02-19 : 13:37:59
|
| thanks sodeep |
 |
|
|
bklr
Master Smack Fu Yak Hacker
1693 Posts |
Posted - 2009-02-20 : 00:01:55
|
| try this tooselect id from table1 where id not in (select id from table2) |
 |
|
|
Nageswar9
Aged Yak Warrior
600 Posts |
Posted - 2009-02-20 : 01:54:39
|
| Also try thisselect id from @temp t where not exists (select id from @temp1 where id = t.id) |
 |
|
|
Jai Krishna
Constraint Violating Yak Guru
333 Posts |
Posted - 2009-02-20 : 05:56:26
|
| SELECT t.Id FROM Table1 tLEFT JOIN Table2 t2 ON (t.id = t1.id)WHERE t2.Id IS NULLJai Krishna |
 |
|
|
raky
Aged Yak Warrior
767 Posts |
Posted - 2009-02-20 : 05:59:53
|
quote: Originally posted by bklr try this tooselect id from table1 where id not in (select id from table2)
Replace table1 with table2 and table2 with table1 |
 |
|
|
bklr
Master Smack Fu Yak Hacker
1693 Posts |
Posted - 2009-02-20 : 06:05:07
|
quote: Originally posted by raky
quote: Originally posted by bklr try this tooselect id from table1 where id not in (select id from table2)
Replace table1 with table2 and table2 with table1
hai raky,see this given query is giving correct result declare @tbl1 table (id int)insert into @tbl1 select 1insert into @tbl1 select 2insert into @tbl1 select 3insert into @tbl1 select 4insert into @tbl1 select 5declare @tbl2 table (id int)insert into @tbl2 select 1insert into @tbl2 select 5insert into @tbl2 select 6insert into @tbl2 select 7Select ID from @tbl1exceptSelect Id from @tbl2 select id from @tbl1 where id not in (select id from @tbl2)see u suggested query select id from @tbl2 where id not in (select id from @tbl1) |
 |
|
|
sodeep
Master Smack Fu Yak Hacker
7174 Posts |
Posted - 2009-02-20 : 06:19:38
|
| You can have cases when not in doesn't work. |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2009-02-20 : 07:41:29
|
quote: Originally posted by sodeep You can have cases when not in doesn't work.
Also it wont throw errors in some casesdeclare @t1 table(i int)insert into @t1 select 10declare @t2 table(j int)insert into @t2select 101--Column i doesnt exist in @t2select * from @t1 where i not in (select i from @t2)MadhivananFailing to plan is Planning to fail |
 |
|
|
sodeep
Master Smack Fu Yak Hacker
7174 Posts |
Posted - 2009-02-20 : 08:08:07
|
| Here is why?http://weblogs.sqlteam.com/mladenp/archive/2007/05/18/60210.aspx |
 |
|
|
|