Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations dencom on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Exclusive records between tables

Status
Not open for further replies.

tbode2006

Programmer
Aug 24, 2006
62
US
Given two tables. The first is a list of 100 words.
The second contains up to 20 words, constantly changing. At any time I would like to know what words exist in the second table that are NOT in the first table. I have been playing with the different type of Joins but without success in getting the ones that are NOT in the large table. I can get the ones in common. What am I missing?
 
This won't use an index, but for a table that small, it shouldn't matter:

Code:
Select col1 from table2 where col1 not in (select col1 from table1)

table1 is the 100 word table

table2 is the 20 word table

[monkey][snake] <.
 
What is in Table2 But Not in Table1...

Code:
[COLOR=blue]Select[/color] Table2.Word
[COLOR=blue]From[/color]   Table2
       [COLOR=#FF00FF]Left[/color] [COLOR=blue]Join[/color] Table1
         [COLOR=blue]On[/color] Table2.Word = Table1.Word
[COLOR=blue]Where[/color]  Table1.Word [COLOR=blue]Is[/color] NULL

-George

Strong and bitter words indicate a weak cause. - Fortune cookie wisdom
 
Now, I want to delete the table 1 records that are not in table 2 and am having trouble with the syntax.
Any suggestions?
 
Code:
Delete t2
From   Table2 t2
       Left Join Table1 t1
         On T2.Word = T1.Word
Where  T1.Word Is NULL

As always with a delete statment, test to make sure it is doing what you want before running on production data.

"NOTHING is more important in a database than integrity." ESquared
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top