开发者

SELECT * FROM Table WHERE col has at least 3 identical

I need to a MySQL query that returns the results of a table where the elements of a specific column are identical in at least 'n' records.

example pseudocode:

开发者_开发问答SELECT * FROM Table WHERE col has at least 3 identical


select * from table where col in (
    select col from table group by col having count(*) > 3
)


SELECT col, COUNT(*) AS total FROM tbl GROUP BY col HAVING total >= 3


select * from table a
inner join (
    select col,n=count(*)
      from table
     group by col
    having count(*) >= 3 ) as b     
on (a.col=b.col)


mysql allows you to write something like the following:

select *
from table
group by col
having count(*) >= 3

other dbmss force you to specify all columns explicitly and use MAX or MIN on them


SELECT DISTINCT * FROM TableName t WHERE 2 < (SELECT COUNT(*) FROM TableName p WHERE p.col = t.col)
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜