使用CAST()
在MAX()
获得从数字VARCHAR类型和值最大的ID。让我们首先创建一个表。在这里,我们有一列具有varchar类型-
mysql> create table DemoTable ( UserMarks varchar(20) );
使用插入命令在表中插入一些记录-
mysql> insert into DemoTable values('77'); mysql> insert into DemoTable values('98'); mysql> insert into DemoTable values('45'); mysql> insert into DemoTable values('56'); mysql> insert into DemoTable values('89'); mysql> insert into DemoTable values('99'); mysql> insert into DemoTable values('91'); mysql> insert into DemoTable values('94');
以下是使用select语句显示表中所有记录的查询-
mysql> select *from DemoTable;
这将产生以下输出-
+-----------+ | UserMarks | +-----------+ | 77 | | 98 | | 45 | | 56 | | 89 | | 99 | | 91 | | 94 | +-----------+ 8 rows in set (0.00 sec)
以下是从varchar类型和数值中获取最大ID的查询-
mysql> SELECT MAX(CAST(UserMarks AS SIGNED)) AS MAX_VALUE from DemoTable;
这将产生以下输出-
+-----------+ | MAX_VALUE | +-----------+ | 99 | +-----------+ 1 row in set (0.00 sec)