使用 MySQL 中的字符串和数字对 VARCHAR 记录排序
为此,请使用 ORDER BY 子句。让我们首先创建一个表 -
mysql> create table DemoTable -> ( -> StudentCode varchar(20) -> ); Query OK, 0 rows affected (0.57 sec)
使用 insert 命令在表中插入一些记录 -
mysql> insert into DemoTable values('101J'); Query OK, 1 row affected (0.18 sec) mysql> insert into DemoTable values('100A'); Query OK, 1 row affected (0.10 sec) mysql> insert into DemoTable values('100B'); Query OK, 1 row affected (0.09 sec) mysql> insert into DemoTable values('101S'); Query OK, 1 row affected (0.16 sec) mysql> insert into DemoTable values('103M'); Query OK, 1 row affected (0.15 sec)
使用 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)
广告