[英]MySQL Join Multiple IDs From Another Table
I've spent hours trying to work out how to do this for my project, but I can't seem to find either, a good way of explaining it in order to research the correct thing, or an answer to my question, hence the post here.我花了几个小时试图弄清楚如何为我的项目做这件事,但我似乎也找不到解释它以研究正确事物的好方法,或者我的问题的答案,因此在这里发帖。
I have two tables as shown below, one with an ID and the name, and another with fields relating to those IDs.我有两个表,如下所示,一个带有 ID 和名称,另一个带有与这些 ID 相关的字段。 I need to be able to match the ID to the name.
我需要能够将 ID 与名称匹配。
Table 1:表格1:
ID | Name
---|----------
1 | Square
2 | Rectangle
3 | Circle
Table 2:表 2:
ID | Field1ID | Field1Data | Field2ID | Field2Data | Field3ID | Field3Data
---|----------|------------|----------|------------|----------|-----------
1 | 1 | Red | 3 | Green | 2 | Blue
I need to be able to match the FieldxID's in table 2 to the name's in table 1 creating the following:我需要能够将表 2 中的 FieldxID 与表 1 中的名称相匹配,从而创建以下内容:
ID | Field1ID | Field1Data | Field2ID | Field2Data | Field3ID | Field3Data
---|----------|------------|----------|------------|-----------|-----------
1 | Square | Red | Circle | Green | Rectangle | Blue
Any help would be appreciated.任何帮助,将不胜感激。
you need several joins, one for each fieldID您需要多个连接,每个 fieldID 一个
select b.Name, a.Field1Data, c.name, a.Field2Data, d.name, a.FieldeData
from table2 a
inner join table1 b on b.id = a.Field1ID
inner join table1 c on c.id = a.Field2ID
inner join table1 d on d.id = a.Field3ID
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.