简体   繁体   English

将一个表中的多个列连接到另一个表中的单个列

[英]Join multiple columns from one table to single column from another table

I'm trying to learn how to join multiple columns from one table to a single column from another table. 我正在尝试学习如何将一个表中的多个列连接到另一个表中的单个列。

This is my table structure in its simplest form: 这是我最简单形式的表结构:

teams 球队

id | team_name |
1  |   teamA   |
2  |   teamB   |
3  |   teamC   |
4  |   teamD   |

trades 交易

id |  team_1 (FK to teams.id)  |  team_2 (FK to teams.id)  |
1  |            1              |              2            |
2  |            3              |              4            |

This is my current SQL which joins trades.team_1 to teams.id: 这是我当前的SQL,它将trades.team_1加入teams.id:

SELECT teams.team_name AS team1, teams.team_name AS team2, trades.team_1, trades.team_2
FROM teams
JOIN trades ON (trades.team_1 = teams.id);

My question is, how do I create a second join that also joins trades.team_2 to trades.id? 我的问题是,如何创建第二个连接,也将trades.team_2加入trades.id?

This would mean both trades.team_1 AND trades.team_2 would be joined to trades.id 这意味着trades.team_1 AND trades.team_2将加入trades.id

The results I want to get back would be: 我想回来的结果是:

team1  |  team2  |  team_1  |  team_2  |
teamA  |  teamB  |    1     |     2    |
teamC  |  teamD  |    3     |     4    |

Like this: 像这样:

select t1.team_name as team1, t2.team_name as team2, t.team_1, t.team_2
from trades t
inner join teams t1 on t1.id = t.team_1
inner join teams t2 on t2.id = t.team_2;
SELECT t1.team_name AS team1, t2.team_name AS t2, tr.team_1, tr.team_2
FROM trades tr
INNER JOIN teams t1 ON t1.id = tr.team_1
INNER JOIN teams t2 ON t2.id = tr.team_2

Try joining the teams table again but using two different aliases: 尝试再次加入teams表,但使用两个不同的别名:

SELECT
    teams1.team_name AS team1,
    teams2.team_name AS team2,
    trades.team_1,
    trades.team_2
FROM trades
JOIN teams AS teams1 ON trades.team_1 = teams1.id
JOIN teams AS teams2 ON trades.team_2 = teams2.id

You need to join twice: 你需要加入两次:

SELECT t1.team_name as team1, t2.team_name as team2, trades.team_t, trades.team_2 
FROM teams t1, teams t2, trades 
WHERE t1.id = trades.team_1 and t2.id = trades.team_2

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

相关问题 将一个表的两列连接到另一表的一列 - Join two columns from one table to one column from another 如何将一列连接到另一个表中的所有列 - how to join one column to all columns from another table 内部连接第一个表中的多个列到第二个表中的单个列 - inner join multiple columns from first table to a single column from second table 如何将一个表中的两个或更多列与另一个表中的一个列联接在一起,即使第一个表列中的值为NULL - How to join two or more columns from one table with one column from another table, even there are NULL values in first table columns 将列从一个表连接到另一个表CodeIgniter中的另一列 - Join column from one table to another column in other table CodeIgniter 联接并显示一个表中的2列被另一个表引用 - Join and Display 2 Columns from One Table refer by Another Table 将单个表的单列连接到另一个表的多个列 - Join single column of single table to multiple column of another table 从一个列中搜索与另一个列中的单个值匹配的多个值(同一表) - Searching multiple values from one column that match a single value from another column (same table) 将数据从另一表的一列插入到同一表的多列中 - Inserting data into multiple columns of same table from one column of another table 将表1中的多个列合并到表2中的一列 - merging multiple Columns from table1 into one column in table2
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM