MYSQL选择count> 1

时间:2012-07-30 13:07:45

标签: mysql count

需要执行查询才能找到第3列值计数>的行1.示例:

表:

     col1   col2    col3  
------------------------  
1.   val1   val2    val3
2.  val11  val21    val3
3.  val12  val22   val31
4.  val13  val23   val32
5.  val14  val24   val32

结果将是1,2,4,5行,因为我们在col3中的值为count> 1

2 个答案:

答案 0 :(得分:7)

你的问题很不清楚,这可能就是你所要求的:

SELECT COUNT(col3) as countCol3
FROM table
GROUP BY col3
HAVING countCol3 > 1

答案 1 :(得分:0)

这是一个可以帮助你的答案。我已经包含了一些示例数据,以帮助其他人找到一些有用的示例数据:

drop table if exists valTable;

create table valTable
(
id int unsigned primary key auto_increment not null,
col1 varchar(50),
col2 varchar(50),
col3 varchar(50)
);

insert into valTable (col1,col2,col3) values ('val1','val2','val3');
insert into valTable (col1,col2,col3) values ('val11','val21','val3');
insert into valTable (col1,col2,col3) values ('val12','val22','val31');
insert into valTable (col1,col2,col3) values ('val13','val23','val32');
insert into valTable (col1,col2,col3) values ('val14','val24','val32');

select vt.*
from valTable vt
where vt.col3 in
(select col3Multi from (
select col3 col3Multi,count(*) as c
from valTable
group by col3
having c > 1) t );