从 MySQL 中的 VARCHAR 列查找最大值
mysqlmysqli database更新于 2023/10/24 10:43:00
要查找最大值,请使用 MAX() 和 CAST(),因为值是 VARCHAR 类型。我们首先创建一个表 −
mysql> create table DemoTable2030 -> ( -> Value varchar(20) -> ); Query OK, 0 rows affected (0.44 sec)
使用 insert 命令在表中插入一些记录 −
mysql> insert into DemoTable2030 values('8'); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable2030 values('1'); Query OK, 1 row affected (0.19 sec) mysql> insert into DemoTable2030 values('10001'); Query OK, 1 row affected (0.11 sec) mysql> insert into DemoTable2030 values('901'); Query OK, 1 row affected (0.22 sec)
使用 select 语句显示表中的所有记录 &miuns;
mysql> select *from DemoTable2030
这将产生以下输出 −
+-------+ | Value | +-------+ | 8 | | 1 | | 10001 | | 901 | +-------+ 4 rows in set (0.00 sec)
以下是从 VARCHAR 列中查找最大值的查询 −
mysql> select max(cast(Value as unsigned)) from DemoTable2030;
这将产生以下输出 −
+------------------------------+ | max(cast(Value as unsigned)) | +------------------------------+ | 10001 | +------------------------------+ 1 row in set (0.00 sec)