为此,请使用ORDER BY子句。让我们首先创建一个表-
mysql> create table DemoTable -> ( -> StudentCode varchar(20) -> );
使用插入命令在表中插入一些记录-
mysql> insert into DemoTable values('101J'); mysql> insert into DemoTable values('100A'); mysql> insert into DemoTable values('100B'); mysql> insert into DemoTable values('101S'); mysql> insert into DemoTable values('103M');
使用select语句显示表中的所有记录-
mysql> select * from DemoTable;
这将产生以下输出-
+-------------+ | StudentCode | +-------------+ | 101J | | 100A | | 100B | | 101S | | 103M | +-------------+ 5 rows in set (0.00 sec)
以下是查询以字符串和数字排序VARCHAR记录的查询-
mysql> select * from DemoTable order by StudentCode+0,StudentCode;
这将产生以下输出-
+-------------+ | StudentCode | +-------------+ | 100A | | 100B | | 101J | | 101S | | 103M | +-------------+ 5 rows in set, 5 warnings (0.00 sec)