SQL_leftjoin和from两个表的区别select * from a, b where a.id = b.id ;
select * from a left join b on a.id=b.id ;
以上两句有什么区别么?好像没任何区别,不知道left join 和from两个表到底有什么区别?
⼀个是普通的联接,结果中的记录在两个表中都有。
⼀个是左外联接,结果中的记录在A表中存在,B表中不⼀定有。相当于a表为主体表,b为辅助表。
例⼦:
mysql> select * from a;
+------+------+
| id | col |
+------+------+
| 1 | 11 |
| 2 | 12 |
| 3 | 13 |
+------+------+
3 rows in set (0.00 sec)
mysql> select * from b;
+------+------+
| id | col |
+------+------+
| 2 | 22 |
| 3 | 23 |
| 5 | 25 |
+------+------+
3 rows in set (0.00 sec)
mysql>
mysql> select * from a,b where a.id=b.id;
+------+------+------+------+
| id | col | id | col |
+------+------+------+------+
| 2 | 12 | 2 | 22 |
| 3 | 13 | 3 | 23 |
+------+------+------+------+
2 rows in set (0.08 sec)
mysql> select * from a left join b on a.id=b.id;
+------+------+------+------+
sql left join 多表连接| id | col | id | col |
+------+------+------+------+
| 1 | 11 | NULL | NULL |
| 2 | 12 | 2 | 22 |
| 3 | 13 | 3 | 23 |
+------+------+------+------+
3 rows in set (0.00 sec)
mysql>
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论