假設我有一張這樣的桌子,
表一:
source
a
b
c
d
表 B:
destination
b
a
d
c
我寫了這個查詢來連接兩個表,
with A as(
select row_number() over() idx, source from a
),
B as (
select row_number() over() idx, destination from b
),
C as (
select A.source, B.destination from A join B on A.idx=B.idx
)
select * from C;
這回傳了下表,
source destination
a b
b a
c d
d c
現在我想從中洗掉反向重復項。我只想保留一個記錄 a, b 而不是 b, a。同樣我只想要 c, d 而不是 d, c。
期望輸出:
source destination
a b
c d
uj5u.com熱心網友回復:
從我的合并表中,如何僅保留有關源和目標的唯一記錄?– 用戶_12
DELETE t1.*
FROM merged_table t1
JOIN merged_table t2 ON t1.src = t2.dst
AND t1.dst = t2.src
AND t1.src > t2.src;
FIDDLE提供一些解釋性查詢。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/328751.html
標籤:mysql
