简体   繁体   English

MySQL连接在多个列上

[英]Mysql join on multiple columns

I am having an issue where I want to join several columns by an id. 我遇到一个问题,我想通过一个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. 我正在尝试创建一个查询,该查询将查看我的第一个表中的每个ID,然后获取每个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! 谢谢!

Join the same table 3 times with different alias names 使用不同的别名将同一张表连接3次

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

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. 这仅执行一次联接,显示您实际需要的2列记录并进行限制,因此用户仅列出1次。 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. 另外,由于如果用户没有名称设置,您可能不想仅返回ID,所以您不希望左联接。

Edit: If you need indexes you can make one on submitter_id, reviewer_id, processor_id if the performance is needed for you. 编辑:如果需要索引,则可以根据需要创建一个索引,包括submitter_id,reviewer_id,processor_id。

You want all in one resultset, so you could use UNION ALL : 您希望将所有结果集中在一个结果集中,因此可以使用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 . 我认为如果您打算仅对某些组使用某些条件,例如where reviewer_id > 100 ,那么这是一种更好的方法。 If not, the Sir. 如果没有,先生。 Egole is cleaner. 埃戈尔比较干净。

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. 结果将是每笔交易一行中有3个ID对应的名称。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM