永发信息网

请教如何取得mysql 排序的序号

答案:2  悬赏:70  手机版
解决时间 2021-01-03 05:50
  • 提问者网友:雪舞兮
  • 2021-01-02 07:46
请教如何取得mysql 排序的序号
最佳答案
  • 五星知识达人网友:十鸦
  • 2021-01-02 08:45
mysql> select * from a;
+-----+
| col |
+-----+
| 0 |
| 1 |
| 2 |
| 3 |
| 4 |
| 5 |
| 6 |
| 7 |
+-----+
8 rows in set (0.00 sec)

mysql> set @i := 0; select @i := @i + 1 as `order`, a.* from a order by col desc;
+-------+-----+
| order | col |
+-------+-----+
| 1 | 7 |
| 2 | 6 |
| 3 | 5 |
| 4 | 4 |
| 5 | 3 |
| 6 | 2 |
| 7 | 1 |
| 8 | 0 |
+-------+-----+
8 rows in set (0.00 sec)

mysql>
全部回答
  • 1楼网友:平生事
  • 2021-01-02 09:03
参考mysql官方的回答: 当你的表示myisam时: select * from tbl -- this will do a "table scan". if the table has never had any deletes/replaces/updates, the records will happen to be in the insertion order, hence what you observed. 大致意思为,一个myisam引擎表在没有任何的删除,修改操作下,执行 select 不带order by,那么会按照插入顺序进行排序。 if you had done the same statement with an innodb table, they would have been delivered in primary key order, not insert order. again, this is an artifact of the underlying implementation, not something to depend on. 对于innodb引擎表来说,在相同的情况下,select 不带order by,会根据主键来排序,从小到大
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯