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)
精彩评论