返回 MySQL 中计数最多的字段
要返回计数最多的字段,使用 ORDER BY COUNT(*)。我们先创建一个表 −
mysql> create table DemoTable1940 ( FirstName varchar(20) ); Query OK, 0 rows affected (0.00 sec)
使用 insert 命令向表中插入一些记录 −
mysql> insert into DemoTable1940 values('Chris'); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1940 values('Mike'); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1940 values('Adam'); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1940 values('Mike'); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1940 values('Chris'); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1940 values('David'); Query OK, 1 row affected (0.00 sec) mysql> insert into DemoTable1940 values('Mike'); Query OK, 1 row affected (0.00 sec)
使用 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)
广告