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

Multiple JOINS 1

Status
Not open for further replies.

flyadvertising

Programmer
Feb 5, 2011
8
US
Having a problem combining 2 JOINS. Both statements below work. I have also included the relevant fields in the 2 DBs. My question is:
1) How do I combine the 2 statements into 1 statement?
2) How do I reference the rows in my php to distinguish if the result was from the mmpreID JOIN or the mmparID JOIN?

Thanks

DB: matchmaker
mmpreID
mmparID

DB: users
uID
ufname
ulname

SELECT matchmaker.mmpreID, matchmaker.mmparID, users.uID, users.ufname, users.ulname
FROM matchmaker
LEFT JOIN users ON matchmaker.mmparID = users.uID


SELECT matchmaker.mmpreID, matchmaker.mmparID, users.uID, users.ufname, users.ulname
FROM matchmaker
LEFT JOIN users ON matchmaker.mmpreID = users.uID
 
Code:
SELECT matchmaker.mmparID AS matchmaker_user
     , 'par'              AS user_type
     , users.uID
     , users.ufname
     , users.ulname 
  FROM matchmaker 
LEFT OUTER
  JOIN users 
    ON users.uID = matchmaker.mmparID
UNION ALL
SELECT matchmaker.mmpreID AS matchmaker_user
     , 'pre'              AS user_type
     , users.uID
     , users.ufname
     , users.ulname 
  FROM matchmaker 
LEFT OUTER
  JOIN users 
    ON users.uID = matchmaker.mmpreID
you might want to make these INNER JOINs in the UNION

r937.com | rudy.ca
Buy my new book Simply SQL from Amazon
 
Thanks, that worked but the results are alternating instead of being combined into the same row. See below for simple example:

users DB columns:
uID | ufname
1 | John
2 | Mike

matchmaker DB columns:
mmparID | mmpreID
1 | 2

I want to show 'John' and 'Mike' in the same row in my results.
 
ah, you wanted them combined on one row per match

earlier, you asked how to reference the rows in php

that was what [red]'par' AS user_type[/red] was for

:)

r937.com | rudy.ca
Buy my new book Simply SQL from Amazon
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top