如何在MySQL中订购字母数字列?

要订购具有“ 100X,“ 2Z”等)值的字母数字列,请使用ORDER BY。让我们首先创建一个表-

mysql> create table DemoTable
-> (
-> StudentId varchar(100)
-> );

使用插入命令在表中插入一些记录-

mysql> insert into DemoTable values('2X');

mysql> insert into DemoTable values('100Y');

mysql> insert into DemoTable values('100X');

mysql> insert into DemoTable values('2Z');

mysql> insert into DemoTable values('2Y');

mysql> insert into DemoTable values('100Z');

使用select语句显示表中的所有记录-

mysql> select *from DemoTable;

输出结果

这将产生以下输出-

+-----------+
| StudentId |
+-----------+
| 2X        |
| 100Y      |
| 100X      |
| 2Z        |
| 2Y        |
| 100Z      |
+-----------+
6 rows in set (0.00 sec)

这是在MySQL中按字母数字列排序的查询-

mysql>select *from DemoTable order by (StudentId+0), right(StudentId, 1);

输出结果

这将产生以下输出-

+-----------+
| StudentId |
+-----------+
| 2X        |
| 2Y        |
| 2Z        |
| 100X      |
| 100Y      |
| 100Z      |
+-----------+
6 rows in set, 6 warnings (0.00 sec)