MySQL获得单独列中不同值的最大重复出现次数
为此使用COUNT()函数和GROUPBY子句。让我们首先创建一个表-
create table DemoTable -> ( -> Name varchar(100) -> );
使用插入命令在表中插入一些记录-
insert into DemoTable values('John'); insert into DemoTable values('Sam'); insert into DemoTable values('John'); insert into DemoTable values('Mike'); insert into DemoTable values('John'); insert into DemoTable values('Sam'); insert into DemoTable values('Carol');
使用select语句显示表中的所有记录-
select *from DemoTable;
输出结果
这将产生以下输出-
+-------+ | Name | +-------+ | John | | Sam | | John | | Mike | | John | | Sam | | Carol | +-------+ 7 rows in set (0.00 sec)
这是查询SQL中不同值的最大数量的查询-
select Name, count(*) as freq from DemoTable group by Name order by count(*) desc limit 1;
输出结果
这将产生以下输出-
+------+------+ | Name | freq | +------+------+ | John | 3 | +------+------+ 1 row in set (0.00 sec)