在 MySQL 中使用字符串和数字对 VARCHAR 记录进行排序
mysqlmysqli database更新于 2025/7/30 10:52:17
为此,请使用 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)
相关文章
有用资源
mysql 参考教程 - 该教程包含有关 mysql 的更多信息:https://www.cainiaomax.com/mysql/

