下面是测试表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)