连接 2 个表并根据多个列选择重复条目
Joining 2 Tables and selecting duplicate entries based on several columns
我加入了两个表,现在我想根据两个表中的条件显示所有重复条目(不是单个条目,而是两个条目)。
表 1:
Material_ID
Plant
Storage_old
Stock
1234
1
GH65
5
1234
1
ZG43
10
5436
1
GH65
65
表 2:
Plant
Storage_old
Storage_new
1
GH65
ZT65
1
ZG43
ZT65
1
GH65
OE86
我希望 select 的结果是这样的
Material_ID
Plant
Storage_old
Storage_new
Stock
1234
1
GH65
ZT65
5
1234
1
ZG43
ZT65
10
我尝试使用 select
select
t1.material_id,
t1.plant_id ,
t1.storage_old,
t2.storage_new,
t1.stock
from
t1
left join
t2
on
t1.plant = t2.plant
and t1.storage_old = t2.storage_old
group by
t1.material_id,
t1.plant_id ,
t2.storage_new
having
count(*) > 1
没有成功。
如何在不 selecting 所有列的情况下使用组?
非常感谢!
考虑:
查询 1:
SELECT t1.plant, t2.storage_new, t1.material_id
FROM Table1 AS t1 LEFT JOIN Table2 AS t2 ON (t1.plant = t2.plant) AND (t1.storage_old = t2.storage_old)
GROUP BY t1.plant, t2.storage_new, t1.material_id
HAVING (((Count(*))>1));
查询 2:
SELECT Table1.Material_ID, Table1.Plant, Table1.Storage_old, Query1.storage_new, Table1.Stock
FROM Table1 INNER JOIN Query1 ON Table1.Material_ID = Query1.material_id;
可以在 all-in-one 语句的 Query2 中嵌套 Query1 的 SQL。
我加入了两个表,现在我想根据两个表中的条件显示所有重复条目(不是单个条目,而是两个条目)。
表 1:
Material_ID | Plant | Storage_old | Stock |
---|---|---|---|
1234 | 1 | GH65 | 5 |
1234 | 1 | ZG43 | 10 |
5436 | 1 | GH65 | 65 |
表 2:
Plant | Storage_old | Storage_new |
---|---|---|
1 | GH65 | ZT65 |
1 | ZG43 | ZT65 |
1 | GH65 | OE86 |
我希望 select 的结果是这样的
Material_ID | Plant | Storage_old | Storage_new | Stock |
---|---|---|---|---|
1234 | 1 | GH65 | ZT65 | 5 |
1234 | 1 | ZG43 | ZT65 | 10 |
我尝试使用 select
select
t1.material_id,
t1.plant_id ,
t1.storage_old,
t2.storage_new,
t1.stock
from
t1
left join
t2
on
t1.plant = t2.plant
and t1.storage_old = t2.storage_old
group by
t1.material_id,
t1.plant_id ,
t2.storage_new
having
count(*) > 1
没有成功。 如何在不 selecting 所有列的情况下使用组? 非常感谢!
考虑:
查询 1:
SELECT t1.plant, t2.storage_new, t1.material_id
FROM Table1 AS t1 LEFT JOIN Table2 AS t2 ON (t1.plant = t2.plant) AND (t1.storage_old = t2.storage_old)
GROUP BY t1.plant, t2.storage_new, t1.material_id
HAVING (((Count(*))>1));
查询 2:
SELECT Table1.Material_ID, Table1.Plant, Table1.Storage_old, Query1.storage_new, Table1.Stock
FROM Table1 INNER JOIN Query1 ON Table1.Material_ID = Query1.material_id;
可以在 all-in-one 语句的 Query2 中嵌套 Query1 的 SQL。