返回MySQL中具有最高计数的字段
要返回计数最高的字段,请使用ORDERBYCOUNT(*)。让我们首先创建一个表-
mysql> create table DemoTable1940 ( FirstName varchar(20) );
使用插入命令在表中插入一些记录-
mysql> insert into DemoTable1940 values('Chris'); mysql> insert into DemoTable1940 values('Mike'); mysql> insert into DemoTable1940 values('Adam'); mysql> insert into DemoTable1940 values('Mike'); mysql> insert into DemoTable1940 values('Chris'); mysql> insert into DemoTable1940 values('David'); mysql> insert into DemoTable1940 values('Mike');
使用select语句显示表中的所有记录-
mysql> select * from DemoTable1940;
这将产生以下输出-
+-----------+ | FirstName | +-----------+ | Chris | | Mike | | Adam | | Mike | | Chris | | David | | Mike | +-----------+ 7 rows in set (0.00 sec)
这是查询以返回具有最高计数的字段:
mysql> select * from DemoTable1940 group by FirstName order by count(*) desc limit 1;
这将产生以下输出-
+-----------+ | FirstName | +-----------+ | Mike | +-----------+ 1 row in set (0.00 sec)