表连接查询本质是以左边的那个表为准
举例:
mysql> select * from t1;
+------+-------+
| name | value |
+------+-------+
| a | 5 |
| b | 10 |
| c | 15 |
| d | 10 |
+------+-------+
4 rows in set (0.00 sec)
mysql> select * from t2;
+------+-------+
| name | value |
+------+-------+
| b | 5 |
| c | 10 |
| d | 20 |
| e | 99 |
| a | 8 |
| a | 7 |
+------+-------+
6 rows in set (0.00 sec)
mysql> select * from t1
-> left join t2
-> on t1.name = t2.name;
+------+-------+------+-------+
| name | value | name | value |
+------+-------+------+-------+
| b | 10 | b | 5 |
| c | 15 | c | 10 |
| d | 10 | d | 20 |
| a | 5 | a | 8 |
| a | 5 | a | 7 |
+------+-------+------+-------+
5 rows in set (0.00 sec)
此时因为是left join,从结果也能明显的看出是以左边的为基准
mysql> select * from t1
-> right join t2
-> on t1.name = t2.name;
+------+-------+------+-------+
| name | value | name | value |
+------+-------+------+-------+
| a | 5 | a | 8 |
| a | 5 | a | 7 |
| b | 10 | b | 5 |
| c | 15 | c | 10 |
| d | 10 | d | 20 |
| NULL | NULL | e | 99 |
+------+-------+------+-------+
6 rows in set (0.00 sec)
此时right join,所以是以t2为基准的,
t1右连接t2,所以此时t2是在左边的
归根结底:连接查询是以左边的那个表为基准的!
来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/28212711/viewspace-758979/,如需转载,请注明出处,否则将追究法律责任。
转载于:http://blog.itpub.net/28212711/viewspace-758979/