簡體   English   中英

如何返回僅在特定列中存在重復項的行?

[英]How can I return rows where only duplicates within a certain column exist?

假設我的數據庫如下所示:

First Entry | Second Entry | Third Entry
    0              0              0
    0              1              2
    2              1              0 
    2              0              1
    3              0              0

我試圖返回該表的子集,其中FirstEntry列中的元素至少重復一次。 因此,在這種情況下,它將返回除最后一行以外的所有內容。 我該怎么辦? 我嘗試使用諸如count()但是僅設法實現了分組,而不是返回我好奇的實際行(特別是,我不在乎第二和第三項,但僅當第一項重復了至少一次時) 。

使用IN()

select * from your_table
where FirstEntry in
(
   select FirstEntry 
   from your_table
   group by FirstEntry 
   having count(*) > 1
)

或使用JOIN

select t1.* 
from your_table t1
join
(
   select FirstEntry 
   from your_table
   group by FirstEntry 
   having count(*) > 1
) t2 on t1.FirstEntry = t2.FirstEntry 

嘗試這個:

select *
from my_table
where First_Entry IN(SELECT First_Entry From my_table 
group by First_entry having count(*) > 1)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM