如何有效地选择所有重复项

nat*_*vda 10 sql postgresql

我想选择具有表中已存在的值的所有行.我找不到更好的解决方案

select * 
from provisioning_requests tt 
where code in (select code 
               from provisioning_requests tt2 
               where tt2.id <> tt.id)
Run Code Online (Sandbox Code Playgroud)

这似乎有点幼稚.有人有更好的解决方案吗?

nik*_*trs 17

select * 
from provisioning_requests t1
 join (select code from provisioning_requests group by code having count(*)>1) t2
 ON t1.code = t2.code
Run Code Online (Sandbox Code Playgroud)

要么

select * 
from provisioning_requests
 WHERE code in (select code from provisioning_requests group by code having count(*)>1)
Run Code Online (Sandbox Code Playgroud)


Cyr*_*don 9

自动加入完成工作

select tt.* 
from provisioning_requests tt 
    INNER JOIN provisioning_requests tt2 
        ON tt.code = tt2.code
        AND tt2.id <> tt.id
Run Code Online (Sandbox Code Playgroud)