[英]How can I create a MySQL JOIN query for only selecting the rows in one table where a certain number of references to that row exist in another table?
[英]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.