user4780686
user4780686

Reputation:

Mysql join on multiple columns

I am having an issue where I want to join several columns by an id.

My first table looks like this:

submitter_id   reviewer_id   processor_id
75             34            91

The table that I want to join looks like this:

id   first_name   last_name
75   Bob          Smith
34   Albert       McDonald
91   Joe          Blo

I am trying to create a query that will look at each id in my first table and then get the first and last name's for each id.

For example, a query that does this should return something like:

[
    75 => "Bob Smith",
    34 => "Albert McDonald",
    91 => "Joe Blo"
];

Can anybody help me construct a query that can accomplish this? Thanks!

Upvotes: 0

Views: 69

Answers (4)

Sir. Egole
Sir. Egole

Reputation: 118

I think what your actually looking for is more like this:

SELECT n.id, CONCAT(n.first_name, ' ', n.last_name)
  FROM names n
  JOIN ids i
    ON n.id = i.submitter_id
    OR n.id = i.reviewer_id
    OR n.id = i.processor_id
 GROUP BY n.id;

This is only doing one join, shows the records with the 2 columns you actually want and restricts so users are only listed 1 time. Also since you probably don't want to return just the ID if the user doesn't have a name setup you don't want a left join.

Edit: If you need indexes you can make one on submitter_id, reviewer_id, processor_id if the performance is needed for you.

Upvotes: 1

Abkarino
Abkarino

Reputation: 1446

Assuming that you need one row in the first table to return one row at the result.

SELECT tt.submitter_id , CONCAT(ta.first_name, ' ', ta.last_name) , 
       tt.reviewer_id  , CONCAT(tb.first_name, ' ', tb.last_name) ,
       tt.processor_id , CONCAT(tc.first_name, ' ', tc.last_name) 
FROM `transaction` as tt , `names` as ta , `names` as tb , `names` as tc 
WHERE tt.submitter_id = ta.id AND 
      tt.reviewer_id  = tb.id AND
      tt.processor_id = tc.id 

The result would be the 3 ids with the corresponding names in one row per transaction.

Upvotes: 0

Felippe Duarte
Felippe Duarte

Reputation: 15131

You want all in one resultset, so you could use UNION ALL:

SELECT p.id, CONCAT(p.first_name, ' ', p.last_name) as name
  FROM person p
  JOIN firstTable f
    ON p.id = f.submitter_id

UNION ALL

SELECT p.id, CONCAT(p.first_name, ' ', p.last_name) as name
  FROM person p
  JOIN firstTable f
    ON p.id = f.reviewer_id

UNION ALL

SELECT p.id, CONCAT(p.first_name, ' ', p.last_name) as name
  FROM person p
  JOIN firstTable f
    ON p.id = f.processor_id

I think this is a better approach if you plan to use some conditions only for some group, like where reviewer_id > 100. If not, the Sir. Egole is cleaner.

Upvotes: 0

juergen d
juergen d

Reputation: 204924

Join the same table 3 times with different alias names

select t1.submitter_id, t1.reviewer_id, t1.processor_id,
       t2.first_name as submitter_firstname, t2.last_name as submitter_lastname,
       t3.first_name as reviewer_firstname, t3.last_name as reviewer_lastname,
       t4.first_name as processor_firstname, t4.last_name as processor_lastname
from firstTable t1
left join namesTable t2 on t1.submitter_id = t2.id
left join namesTable t3 on t1.reviewer_id = t3.id
left join namesTable t4 on t1.processor_id = t4.id

Upvotes: 2

Related Questions