为MySQL表中的每个值选择最大值?
为此,请结合使用GROUP BY子句MAX()
。让我们首先创建一个表-
mysql> create table DemoTable-> (
-> CountryName varchar(20),
-> Population int
-> );
使用插入命令在表中插入一些记录-
mysql> insert into DemoTable values('US',560);mysql> insert into DemoTable values('UK',10090);
mysql> insert into DemoTable values('UK',8794);
mysql> insert into DemoTable values('US',1090);
使用select语句显示表中的所有记录-
mysql> select *from DemoTable;
这将产生以下输出-
+-------------+------------+| CountryName | Population |
+-------------+------------+
| US | 560 |
| UK | 10090 |
| UK | 8794 |
| US | 1090 |
+-------------+------------+
4 rows in set (0.00 sec)
以下是查询以选择MySQL表中每个值的最大值-
mysql> select CountryName,max(Population) from DemoTable group by CountryName;
这将产生以下输出-
+-------------+-----------------+| CountryName | max(Population) |
+-------------+-----------------+
| US | 1090 |
| UK | 10090 |
+-------------+-----------------+
2 rows in set (0.00 sec)
以上是 为MySQL表中的每个值选择最大值? 的全部内容, 来源链接: utcz.com/z/350173.html