SELECT * FROM表WHERE col至少有3个相同

时间:2011-06-20 17:16:14

标签: mysql

我需要一个MySQL查询,它返回一个表的结果,其中特定列的元素在至少'n'个记录中是相同的。

示例伪代码:

SELECT * FROM Table WHERE col has at least 3 identical

5 个答案:

答案 0 :(得分:4)

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

答案 1 :(得分:2)

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

答案 2 :(得分:0)

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)

答案 3 :(得分:0)

mysql允许您编写如下内容:

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

其他dbmss强制您明确指定所有列并对其使用MAXMIN

答案 4 :(得分:0)

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