Connecting Tech Pros Worldwide Forums | Help | Site Map

joining multiple tables based on 1 criteria

Newbie
 
Join Date: Apr 2008
Posts: 31
#1: Jul 31 '08
Apparently, Im incapable of properly executing this query! I've been at this for far too long and gotten nowhere. Forgive me if this is a umb mistake; I'm not great at SQL.

I've got three tables, MASTER, RETAIL, and AUDIT... MASTER and RETAIL have the same fields, except each record has a unique ITEMNO, and master's numbers are positive and retail's are negative. AUDIT contains some of both table's item numbers in it. I just want to join audit to both tables based on the ITEMNO, (Audit's ITEMNO field is actually called MMNO, if that matters) I can JOIN one or the other and it'll show up with the right fields, but when I try to join both it returns with no records. I know I'm messing up, I just don't know how to fix it.

This is what I've got (take out either master OR retail and it works fine):
SELECT AUDIT.TRANSACTIONNO,AUDIT.DATIM, AUDIT.MMNO, AUDIT.COPYS, AUDIT.PRICE,
MASTER.ITEMNO, MASTER.MTITLE, MASTER.MSRP,
RETAIL.ITEMNO, RETAIL.MTITLE, RETAIL.MSRP,
FROM BUYBACKAUDIT
JOIN RETAIL ON AUDIT.ITEMNO=RETAIL.MMBSNO
JOIN MASTER ON AUDIT.ITEMNO=MASTER.MMBSNO

Any help would be much appreciated. Thanks!

ck9663's Avatar
Expert
 
Join Date: Jun 2007
Posts: 1,925
#2: Jul 31 '08

re: joining multiple tables based on 1 criteria


What's in AUDIT? Positive or Negative? or both?

-- CK
Newbie
 
Join Date: Apr 2008
Posts: 31
#3: Jul 31 '08

re: joining multiple tables based on 1 criteria


both. So I want to see what item numbers are in AUDIT and match them to the master and retail records accordingly.
ck9663's Avatar
Expert
 
Join Date: Jun 2007
Posts: 1,925
#4: Jul 31 '08

re: joining multiple tables based on 1 criteria


Use left join on both, instead of inner join

-- CK
Newbie
 
Join Date: Aug 2008
Posts: 3
#5: Aug 4 '08

re: joining multiple tables based on 1 criteria


Hi

use the following query

SELECT AUDIT.TRANSACTIONNO,AUDIT.DATIM, AUDIT.MMNO, AUDIT.COPYS, AUDIT.PRICE,
MASTER.ITEMNO, MASTER.MTITLE, MASTER.MSRP,
RETAIL.ITEMNO, RETAIL.MTITLE, RETAIL.MSRP,
FROM BUYBACKAUDIT AUDIT
LEFT OUTER JOIN RETAIL ON AUDIT.ITEMNO=RETAIL.MMBSNO
LEFT OUTER JOIN MASTER ON AUDIT.ITEMNO=MASTER.MMBSNO

Thanks
Nathan
Hyper Drive
Bangalore
Reply