MySQL5.7,left join关联排序查询,多次查询的顺序竟然不一样?

下面是测试表t_a、t_b;innodb,都只有主键索引,sort_str为varchar类型,sort_int为int类型

mysql> select * from t_a;
+----+-------+----------+----------+---------+------+
| id | name  | sort_int | sort_str | exam_id | w_id |
+----+-------+----------+----------+---------+------+
|  1 | a     |      111 | 111      |    1001 |    1 |
|  2 | bb    |      111 | 111      |    1002 |    2 |
|  3 | ccc   |      111 | 111      |    1003 |    3 |
|  4 | dddd  |      111 | 111      |    1004 |    4 |
|  5 | eeeee |      111 | 111      |    1005 |    5 |
+----+-------+----------+----------+---------+------+
5 rows in set (0.00 sec)

mysql> select * from t_b;
+---------+---------+----------+----------+----------+------+
| exam_id | project | sort_int | sort_str | other_id | u_id |
+---------+---------+----------+----------+----------+------+
|       1 | 语文    |      111 | 111      |     1001 |    1 |
|       2 | 数学    |      111 | 111      |     1002 |    2 |
|       3 | 生物    |      111 | 111      |     1003 |    3 |
|       4 | 化学    |      111 | 111      |     1004 |    4 |
|       5 | 物理    |      111 | 111      |     1005 |    5 |
+---------+---------+----------+----------+----------+------+
5 rows in set (0.00 sec)

下面通过左连接查询,多次查询发现,相同的SQL,返回的结果顺序顺序会不同,请教一下这种情况的原理是咋回事。。关联字段没有索引,不是主键。

mysql> SELECT a.id,a.`name`,a.sort_int,a.sort_str,b.project from t_a a LEFT JOIN t_b b ON a.w_id=b.u_id where a.sort_int='111' ORDER by a.sort_int limit 5;
+----+-------+----------+----------+---------+
| id | name  | sort_int | sort_str | project |
+----+-------+----------+----------+---------+
|  3 | ccc   |      111 | 111      | 生物    |
|  4 | dddd  |      111 | 111      | 化学    |
|  5 | eeeee |      111 | 111      | 物理    |
|  1 | a     |      111 | 111      | 语文    |
|  2 | bb    |      111 | 111      | 数学    |
+----+-------+----------+----------+---------+
5 rows in set (0.00 sec)

mysql> SELECT a.id,a.`name`,a.sort_int,a.sort_str,b.project from t_a a LEFT JOIN t_b b ON a.w_id=b.u_id where a.sort_int='111' ORDER by a.sort_int limit 5;
+----+-------+----------+----------+---------+
| id | name  | sort_int | sort_str | project |
+----+-------+----------+----------+---------+
|  5 | eeeee |      111 | 111      | 物理    |
|  1 | a     |      111 | 111      | 语文    |
|  2 | bb    |      111 | 111      | 数学    |
|  3 | ccc   |      111 | 111      | 生物    |
|  4 | dddd  |      111 | 111      | 化学    |
+----+-------+----------+----------+---------+
5 rows in set (0.00 sec)
 

这是数据库系统的“”故意“”设计。

如果没有指定ORDER BY语句,则数据库不保证以特定顺序返回结果。 
如果指定ORDER BY语句,数据库系统r将对行进行排序,并按请求的顺序返回。

但是,如果该顺序不是确定性的,即可能有重复的值,则在每个具有相同值的组中,由于与上述相同的原因,该顺序是“随机的”
确保确定性顺序的唯一方法是在ORDER BY子句中包含保证的唯一列或列组(例如主键)。

目前这个sort_int的值都是一样的,因此会默认采用主键排序,如果你想实现排序,不如order a.id