显示数字列中 ASC 顺序的 MySQL 查询?
借助 CAST() 函数,可以实现。首先,我们创建一个表 -
mysql> create table DemoTable ( Id int NOT NULL AUTO_INCREMENT PRIMARY KEY, Score int ); Query OK, 0 rows affected (0.72 sec)
使用 insert 命令在表中插入一些记录 -
mysql> insert into DemoTable(Score) values(10); Query OK, 1 row affected (0.19 sec) mysql> insert into DemoTable(Score) values(100); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable(Score) values(11); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable(Score) values(97); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable(Score) values(78); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable(Score) values(89); Query OK, 1 row affected (0.18 sec)
使用 select 语句显示表中的所有记录 -
mysql> select *from DemoTable;
将生成以下输出 -
+----+-------+ | Id | Score | +----+-------+ | 1 | 10 | | 2 | 100 | | 3 | 11 | | 4 | 97 | | 5 | 78 | | 6 | 89 | +----+-------+ 6 rows in set (0.00 sec)
以下是设置数字列中 ASC 顺序的查询 -
mysql> select *from DemoTable order by cast(Score AS SIGNED INTEGER) ASC;
将生成以下输出 -
+----+-------+ | Id | Score | +----+-------+ | 1 | 10 | | 3 | 11 | | 5 | 78 | | 6 | 89 | | 4 | 97 | | 2 | 100 | +----+-------+ 6 rows in set (0.00 sec)
广告