不带标识符对 MySQL 结果排序?
若要对不带标识符的 MySQL 结果进行排序,语法如下 −
select * from yourTableName order by 1 DESC LIMIT yourLimitValue;
首先创建一张表 −
mysql> create table DemoTable1325 -> ( -> Id int, -> Name varchar(20) -> ); Query OK, 0 rows affected (0.69 sec)
使用插入命令在表中插入一些记录 −
mysql> insert into DemoTable1325 values(100,'Chris'); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable1325 values(101,'Bob'); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable1325 values(120,'David'); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable1325 values(115,'Sam'); Query OK, 1 row affected (0.18 sec) mysql> insert into DemoTable1325 values(130,'Carol'); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable1325 values(150,'Mike'); Query OK, 1 row affected (0.15 sec)
使用选择语句显示表中的所有记录 −
mysql> select * from DemoTable1325;
将产生以下输出 −
+------+-------+ | Id | Name | +------+-------+ | 100 | Chris | | 101 | Bob | | 120 | David | | 115 | Sam | | 130 | Carol | | 150 | Mike | +------+-------+ 6 rows in set (0.00 sec)
以下是对不带标识符的 MySQL 结果进行排序的查询 −
mysql> select * from DemoTable1325 order by 1 DESC LIMIT 4;
将产生以下输出
+------+-------+ | Id | Name | +------+-------+ | 150 | Mike | | 130 | Carol | | 120 | David | | 115 | Sam | +------+-------+ 4 rows in set (0.00 sec)
广告