select 结果的排列顺序

 show variables like '%storage_engine%';
+----------------------------+--------+
| Variable_name              | Value  |
+----------------------------+--------+
| default_storage_engine     | InnoDB |
| default_tmp_storage_engine |        |
| enforce_storage_engine     |        |
| storage_engine             | InnoDB |
+----------------------------+--------+
4 rows in set (0.00 sec)

为何没有按照id 的顺序排列?

MariaDB [roll]> select id,stu_id from student;
+-----+--------------+
| id  | stu_id       |
+-----+--------------+
|   1 | 201702070220 |
|   2 | 201702070227 |
|   3 | 201702070304 |
|   4 | 201702070310 |
|   5 | 201702070314 |
|   6 | 201702070320 |
|   7 | 201702070333 |
|   8 | 201702070334 |
|   9 | 201704010225 |
|  10 | 201704020103 |
|  11 | 201602060211 |
|  12 | 201605020114 |
|  13 | 201605080129 |
|  14 | 201603090120 |
|  15 | 201605080238 |
|  16 | 201705020129 |
|  17 | 201705020130 |
|  18 | 201605070108 |
|  19 | 201705030114 |
|  20 | 201705030123 |
|  21 | 201603020113 |
|  22 | 201702010101 |
|  23 | 201703020123 |
|  24 | 201701050106 |
|  25 | 201701050107 |
|  26 | 201701070129 |
|  27 | 201701020212 |
|  28 | 201503060102 |
|  29 | 201503060105 |
|  30 | 201703010107 |
| 107 | 201602050139 |
| 106 | 201602050138 |
| 105 | 201602050137 |
| 104 | 201602050136 |
| 103 | 201602050135 |
| 102 | 201602050134 |
| 101 | 201602050133 |
| 100 | 201602050132 |
|  99 | 201602050131 |
|  98 | 201602050129 |
|  97 | 201602050128 |
|  96 | 201602050127 |
|  95 | 201602050126 |
|  94 | 201602050125 |
|  93 | 201602050124 |
|  92 | 201602050123 |
|  91 | 201602050122 |
|  90 | 201602050121 |
|  89 | 201602050120 |
|  88 | 201602050119 |
|  87 | 201602050118 |
|  86 | 201602050117 |
|  85 | 201602050116 |
|  84 | 201602050114 |
|  83 | 201602050113 |
|  82 | 201602050112 |
|  81 | 201602050111 |
|  80 | 201602050110 |
|  79 | 201602050109 |
|  78 | 201602050108 |
|  77 | 201602050107 |
|  76 | 201602050105 |
|  75 | 201602050104 |
|  74 | 201602050103 |
|  73 | 201602050102 |
|  72 | 201602050101 |
| 108 | 201602050140 |
| 109 | 201602050141 |
| 110 | 201601040110 |
| 111 | 201602040103 |
| 112 | 201602060228 |
| 113 | 201602060205 |
+-----+--------------+
72 rows in set (0.00 sec)

id 可是主键

CREATE TABLE student (
id int(3) NOT NULL AUTO_INCREMENT COMMENT '主键 id',
stu_id varchar(14) NOT NULL COMMENT '学号',
class varchar(10) NOT NULL COMMENT '班级',
stu_name varchar(10) NOT NULL COMMENT '姓名'
PRIMARY KEY (id),
) ENGINE=MyISAM AUTO_INCREMENT=1 DEFAULT CHARSET=utf8

阅读 1.9k
2 个回答

因为你没有加order,mysql没有缺省的order,如果想按id排序,需要手动加order by id

If multiple rows have identical values in the ORDER BY columns, the server is free to return those rows in any order, and may do so differently depending on the overall execution plan. In other words, the sort order of those rows is nondeterministic with respect to the nonordered columns.

文档中Order by字段中有相同值,返回结果排序都是不确定的,推断order by null更是如此

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题