我已經加入了兩個表,現在我想根據兩個表中的標準顯示所有重復的條目(而不是單個條目,而是兩者)。
表格1:
| Material_ID | 植物 | 存盤舊 | 庫存 |
|---|---|---|---|
| 1234 | 1 | GH65 | 5 |
| 1234 | 1 | ZG43 | 10 |
| 5436 | 1 | GH65 | 65 |
表2:
| 植物 | 存盤舊 | 存盤_新 |
|---|---|---|
| 1 | GH65 | ZT65 |
| 1 | ZG43 | ZT65 |
| 1 | GH65 | OE86 |
我希望選擇的結果是這樣的
| Material_ID | 植物 | 存盤舊 | 存盤_新 | 庫存 |
|---|---|---|---|---|
| 1234 | 1 | GH65 | ZT65 | 5 |
| 1234 | 1 | ZG43 | ZT65 | 10 |
我嘗試使用選擇
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
沒有成功。如何在不選擇所有列的情況下使用組?非常感謝!
uj5u.com熱心網友回復:
考慮:
查詢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;
可以將 Query1 的 SQL 嵌套在 Query2 中以實作一體式陳述句。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/471271.html
標籤:毫秒访问
