“JOIN”表的结果包括两个表,其中两列具有使用参考表的值 [英] Result of table of "JOIN"ing two tables including two columns having values using a reference table

查看:50
本文介绍了“JOIN”表的结果包括两个表,其中两列具有使用参考表的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有三个表,包括一个参考表(包括2列Item_IDs和相关的Item_names)和另外两个表,每个表都有一个名为ID1,C1&的列。 ID2,C2。

以下是表格样本结构:



参考表:



| item_id | item_name | .....

|     1      |     name1  | ....

|     2      |     name2  | .....





表1:



|   item_id1  |   C1 |

|   5    | 10  |





表2:



|   item_id2  |   C2 |

|   7    |   10 |





现在,我想加入表格Table1&表2中他们的C1& C2列相等。

我想要的是,在上面(JOIN)查询的结果表的一行中,我想要另外两列,其中一列具有Table1的ID1的item_name和一个具有Table2的ID2的item_name。

有关更多说明,下面是结果表的示例(由两个表的C1和C2相等的行组成的表) :



| item_id1 | Item_Name1 | item_id2 | Item_Name2 |

| 1    |     name1      | 2    | name2 |





感谢您帮我写作一个合适的SQL查询。在此先感谢。

I have three tables including one reference table(including 2 columns Item_IDs with associated Item_names) and two other tables each having a column named ID1,C1 & ID2,C2.
followings are the tables sample structures:

Reference Table:

| item_id | item_name | .....
|     1      |     name1   | ....
|     2      |     name2   | .....
.
.
Table 1:

|   item_id1   |   C1 |
|   5     | 10   |
.
.
Table 2:

|   item_id2   |   C2 |
|   7     |   10 |
.
.
Now, I want to "JOIN" tables Table1 & Table2 where their C1 & C2 columns are equal.
What I want is, in a row of the result table of the above (JOIN) query, I want to have two additional columns one having the item_name of the ID1 of Table1 and one having the item_name of the ID2 of Table2.
For more clarification, below is a sample of the result table(the table that is made of rows in which C1 & C2 of both tables are equal):

| item_id1 | Item_Name1 | item_id2 | Item_Name2 |
| 1     |     name1       | 2     | name2 |
.
.
I appreciate your helping me with writing a suitable sql query. Thanks in advance.

推荐答案

它真的没有什么复杂的。加入C1& ;; C2列。然后连接两次到参考表以检索每个项目ID的项目名称。瞧:

There is really nothing complicated about it. Join the two tables on C1 & C2 columns. Then join twice to Reference Table to retrieve the item name for each item id. And voila:
select 
  t1.item_id1, ref1.item_name as item_name1, 
  t2.item_id2, ref2.item_name as item_name2
from 
  table1 t1 inner join table2 t2 on t2.C2 = t1.C1
  inner join ReferenceTable ref1 on ref1.item_id = t1.item_id1
  inner join ReferenceTable ref2 on ref2.item_id = t2.item_id2


这篇关于“JOIN”表的结果包括两个表,其中两列具有使用参考表的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆