If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > PostgreSQL > inner join with OR cause duplicates

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old
Registered User
 
Join Date: Feb 2010
Posts: 35
Question inner join with OR cause duplicates

I have a query where I have to join one table to another using an OR. For example

select bar.name,
foo.pkid
from foo
inner join bar on (foo.id1 = bar.id or foo.id2 = bar.id)

the problem is it results in duplicate rows because it is matching twice, cases when foo.id1 matches and again for the same foo.pkid when foo.id2 matches.

what I really want is only to return 1 record from foo if either matches.

So far, I've only found distinct to work but would prefer to avoid. I looked at a subquery using EXISTS but I also need columns to be output from bar which makes that difficult.

any suggestions?
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On