开发者

SELECT * FROM Table WHERE col has at least 3 identical

开发者 https://www.devze.com 2023-03-14 00:36 出处:网络
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.

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

精彩评论

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