我想知道是否有办法将自我子查询转换为自联接 这是自我子查询
SELECT a,
b
FROM c AS t1
WHERE ( b IN (SELECT b
FROM c AS t2
WHERE ( t1.b = b )
AND ( t1.e <> e )) )
答案 0 :(得分:4)
如果您只想找到重复项,EXIST
可能会更快:
SELECT a,b FROM c WHERE EXISTS(SELECT NULL FROM c c2 WHERE c2.b=c.b AND c2.e<>c.e)
如果您希望将每个记录与其副本一起加入,但每个记录只能获得一条记录:
select t1.a
, t1.b
, t1.e as t1e
, t2.e as t2e
from c as t1
inner join c as t2
on t1.b = t2.b
and t1.e > t2.e
(请注意,我使用了>
代替<>
)
答案 1 :(得分:2)
由于e
是主键,另一种接近此方法的方法是
SELECT a,
b
FROM (SELECT a,
b,
COUNT(*) OVER (PARTITION BY b) AS Cnt
FROM c) T1
WHERE Cnt > 1
答案 2 :(得分:1)
SELECT t1.a, t2.b
FROM c as t1
join c as t2 on t1.b=t2.b
WHERE t1.e <> t2.e
答案 3 :(得分:1)
select t1.a
, t1.b
from c as t1
join c as t2
on t1.b = t2.b
and t1.e <> t2.e