MySQL 如何从varchar列中获取大于特定值的值
由于要获取大于特定值的值的列是VARCHAR,请使用CAST()函数。例如,要从具有varchar值的列中提取大于999的值。
让我们首先创建一个表-
mysql> create table DemoTable
(
Value varchar(100)
);
Query OK, 0 rows affected (1.02 sec)
使用insert命令在表中插入一些记录-
mysql> insert into DemoTable values('900');
Query OK, 1 row affected (0.49 sec)
mysql> insert into DemoTable values('1090');
Query OK, 1 row affected (0.14 sec)
mysql> insert into DemoTable values('860');
Query OK, 1 row affected (0.25 sec)
mysql> insert into DemoTable values('12345');
Query OK, 1 row affected (0.30 sec)
mysql> insert into DemoTable values('908345');
Query OK, 1 row affected (0.16 sec)
使用select语句从表中显示所有记录-
mysql> select *from DemoTable;
这将产生以下输出-
+--------+
| Value |
+--------+
| 900 |
| 1090 |
| 860 |
| 12345 |
| 908345 |
+--------+
5 行记录 (0.00 秒)
以下是从varchar列中获取大于特定值的值的查询-
mysql> select max(cast(Value AS SIGNED)) from DemoTable;
这将产生以下输出-
+----------------------------+
| max(cast(Value AS SIGNED)) |
+----------------------------+
| 908345 |
+----------------------------+
1 行记录 (0.05 秒)
阅读更多:MySQL 教程
极客教程