获取所有重复行

ncecgwcz  于 2021-07-26  发布在  Java
关注(0)|答案(2)|浏览(215)

我只想获取重复的行,其中name列中的值重复多次,而没有count列,我不想对它们进行分组。举例如下。例如
例如,这是table:-

id |  name  | project 
----+--------+---------
  1 | aditya | java
  2 | aditya | cloud
  3 | bradly | go
  4 | cooper | java
  5 | alexa  | elixir 
  6 | jason  | search
  7 | jason  | java

结果应为:-

id |  name  | project 
----+--------+---------
  1 | aditya | java
  2 | aditya | cloud
  6 | jason  | search
  7 | jason  | java
uxhixvfz

uxhixvfz1#

一个简单的方法是 exists :

select t.*
from t
where exists (select 1 from t t2 where t2.name = t.name and t2.id <> t.id)
order by name;

也可以使用窗口功能:

select t.*
from (select t.*, count(*) over (partition by name) as cnt
      from t
     ) t
where cnt > 1
order by name;
xdnvmnnf

xdnvmnnf2#

你也可以使用 join 或者一个 in ```
select a.*
from t a
inner join t b on a.name=b.name and a.id<>b.id

select *
from t
where name in (select name from t group by name having count(*)>1);

相关问题