2014-07-17 33 views
0

假設我的數據庫看起來像下面這樣:如何返回只有某列中存在重複項的行?

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

我試圖返回此表,其中在FirstEntry列中的元素重複至少一次的子集。所以在這種情況下,它會返回除最後一行外的所有內容。我怎麼去解決這個問題?我嘗試過使用諸如count()之類的東西,但只設法實現分組,而不是返回我感興趣的實際行(特別是,我關心第二和第三條目,但只有當第一條目重複至少一次時)。

回答

4

使用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 
0

試試這個:

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