为此,您可以将ORDER BY与一些聚合函数right()一起使用。让我们首先创建一个表-
create table DemoTable1487 -> ( -> StudentCode text -> );
使用插入命令在表中插入一些记录-
insert into DemoTable1487 values('560'); insert into DemoTable1487 values('789'); insert into DemoTable1487 values('STUDENT78'); insert into DemoTable1487 values('John89'); insert into DemoTable1487 values('Carol101'); insert into DemoTable1487 values('STUDENT98');
使用select语句显示表中的所有记录-
select * from DemoTable1487;
这将产生以下输出-
+-------------+ | StudentCode | +-------------+ | 560 | | 789 | | STUDENT78 | | John89 | | Carol101 | | STUDENT98 | +-------------+ 6 rows in set (0.00 sec)
这是对数据列进行排序以获取最大文本值的查询-
select * from DemoTable1487 where StudentCode LIKE 'STUDENT%' -> order by cast(right(StudentCode,length(StudentCode)-length('STUDENT')) as UNSIGNED) desc -> limit 1;
这将产生以下输出-
+-------------+ | StudentCode | +-------------+ | STUDENT98 | +-------------+ 1 row in set (0.04 sec)