如何从 VARCHAR 列中获取最小值和最大值,并在不同的 MySQL 列中显示结果?
我们先创建一个表 -
mysql> create table DemoTable ( Value varchar(100) ); Query OK, 0 rows affected (0.75 sec)
使用插入命令在该表中插入一些记录 -
mysql> insert into DemoTable values('190'); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable values('230'); Query OK, 1 row affected (0.11 sec) mysql> insert into DemoTable values('120'); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable values('189'); Query OK, 1 row affected (0.19 sec)
使用选择语句显示表中的所有记录 -
mysql> select *from DemoTable;
将会产生以下输出 -
+-------+ | Value | +-------+ | 190 | | 230 | | 120 | | 189 | +-------+ 4 rows in set (0.00 sec)
以下查询可以从 VARCHAR 列中获取最小值和最大值,并在不同的 MySQL 列中显示结果 -
mysql> select min(cast(Value AS SIGNED)) AS Min, max(cast(Value AS SIGNED)) AS Max from DemoTable;
将会产生以下输出 -
+------+------+ | Min | Max | +------+------+ | 120 | 230 | +------+------+ 1 row in set (0.03 sec)
广告