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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Help with a simple compare please! 1

Status
Not open for further replies.

JustKIDn

MIS
May 6, 2002
386
US
I've searched forums and the net for too many hours and can't find the answer to this.

This is one thing I've tried that seems like it should work.

select pkey, name
from tbl_1
where not exists (select * from tbl_2 where lpkey = pkey)

lpkey from tbl_2 is a foreign key to pkey from tbl_1

I just want to list the names of those whose key from tbl_1 are not currently in tbl_2.

Any ideas?

This should be easy but it just escapes me.

Thanks for the help!

____________________________
JustKIDn

____________________________
Families can be together forever...
 
this can be achieved by a simple left outer join checking for no matching row by detecting the null in the lpkey

Code:
select tbl_1.pkey
     , tbl_1.name
  from tbl_1 
left outer
  join tbl_2
    on tbl_1.pkey
     = tbl_2.lpkey 
 where tbl_2.lpkey is null

rudy | r937.com | Ask the Expert | Premium SQL Articles
SQL for Database-Driven Web Sites (course starts January 9 2005)
 
r937,

I need to apologize.
I was about to say it won't work because lpkey is never null.
But as I looked closer at your code, I realized that it sets up a one to one relationship. So if there's no match in lpkey to pkey then that is a null.

I had tried doing it with joins but I hadn't thought of "where lpkey is null"

Thanks, That earned you a S T A R !

[spin] [spin2] [spin]

____________________________
JustKIDn

____________________________
Families can be together forever...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top