SQL答复中包含多个答复

时间:2018-07-20 21:35:00

标签: sql

我很抱歉,我愿意编辑此名称,但不知道为该标题添加标题的最佳方法。

我有一个表table_1(1列),该表具有唯一值(组合名称),我想以此作为条件传递给另一个具有2组UID的表table_2(其中一个是组合UID,另一个是组件UID,它们都在表3中作为项),它们需要从名称为table_3和UID为table_1的第三张表table_2中提取。

我知道那真的很复杂!或者至少是对我来说...

我的查询返回重复的信息,因为我认为我会将相同的信息传递回给自己...

我的SQL看起来像这样:

Select i.item_id as Combo_kit_id, c.item_id as Component_item_id
from table_3 i 
inner join table_1 t on i.item_id = t.item_id 
inner join table_2 a on i.item_uid = a.assembly_uid
inner join table_3 c on a.component_uid = c.item_uid

表_1:

| item_ID |
----------
|  CA123  |
|  CA124  |
|  CA125  |
|  CA126  |
|  CA127  |
|  CA128  |
|  CA129  |
---------

表_2:

| assembly_UID | component_UID |
--------------------------------
|   1234       |      2234     |
|   1234       |      2235     |
|   1236       |      2236     |
|   1236       |      2237     |
|   1239       |      2238     |
|   1239       |      2239     |
|   1243       |      2242     |
|   1243       |      2288     |
--------------------------------

table_3:

| item_ID |   item_UID  |
-------------------------
| CA123   |    1234     |
| CA124   |    1236     |
| CA125   |    1239     |
| CA126   |    1243     |
| CA127   |    2234     |
| CA128   |    2235     |
| CA129   |    2236     |
| CA130   |    2237     |
| CA131   |    2238     |
| CA132   |    2239     |
| CA133   |    2242     |
| CA134   |    2288     |
-------------------------

我的结果是:

| Combo_kit_id | Component_item_id |
-----------------------------------
|  CA123      |   CA127      |
|  CA123      |   CA127      |
|  CA123      |   CA128      |
|  CA123      |   CA128      |
|  CA124      |   CA129      |
|  CA124      |   CA129      |
|  CA124      |   CA130      |
|  CA124      |   CA130      |
|  CA125      |   CA131      |
|  CA125      |   CA131      |
|  CA125      |   CA132      |
|  CA125      |   CA132      |
------------------------------

是否有一种方法不获取添加的重复项?

1 个答案:

答案 0 :(得分:2)

您不能只使用distinct吗?

Select distinct i.item_id as Combo_kit_id, c.item_id as Component_item_id
from table_3 i 
inner join table_1 t on i.item_id = t.item_id 
inner join table_2 a on i.item_uid = a.item_uid
inner join table_1 c on a.component_uid = c.item_uid
相关问题