更改mysql的默认排序行为 [英] Changing default sorting behavior of mysql
问题描述
我的数据库中有一个表.
I have a table in my database.
+--------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+--------+-------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| rollno | int(11) | NO | | NULL | |
| name | varchar(20) | NO | | NULL | |
| marks | int(11) | NO | | NULL | |
+--------+-------------+------+-----+---------+----------------+
默认情况下,如果我查询
By default if I query
select * from students;
显示按id INT (auto-increment)
排序的结果.
+----+--------+------------+-------+
| id | rollno | name | marks |
+----+--------+------------+-------+
| 1 | 65 | John Doe | 89 |
| 2 | 62 | John Skeet | 76 |
| 3 | 33 | Mike Ross | 78 |
+----+--------+------------+-------+
3 rows in set (0.00 sec)
我想更改默认的排序行为并将rollno
设置为默认的排序字段,我该怎么做?
I want to change default sorting behaviour and make rollno
the default sorting field, how do I do this?
推荐答案
没有默认的排序顺序!
数据库以最快的方式返回数据.如果碰巧是它的存储顺序或定义了密钥,则取决于系统.您不能依靠它.
There is no default sort order!
The DB returns the data in the fastest way possible. If this happen to be the order in which it is stored or a key is defined then this is up to the system. You can't rely on that.
考虑一下:如果您不需要对数据库进行默认排序,为什么数据库会默认使用performace对其进行排序.数据库针对速度进行了优化.
Think about it: Why would the DB use performace to order something by default if you don't need it ordered. DBs are optimised for speed.
如果要订购它,则必须在order by
子句中指定它.
If you want it being ordered then you have to specify that in an order by
clause.
这篇关于更改mysql的默认排序行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!