mysql> select table1.id as id,book,author from table1, table2 where table1.id=table2.id;
+------+------+--------+
| id | book | author |
+------+------+--------+
| 2 | c++ | zhang |
| 3 | php | wang |
+------+------+--------+
2 rows in set (0.00 sec)
mysql> select * from table1 inner join table2 using (id);
+------+------+--------+
| id | book | author |
+------+------+--------+
| 2 | c++ | zhang |
| 3 | php | wang |
+------+------+--------+
2 rows in set (0.00 sec)
mysql> select * from table1 natural join table2;
mysql> select * from table1 inner join table2 using (id);
结果集:
+------+------+--------+
| id | book | author |
+------+------+--------+
| 2 | c++ | zhang |
| 3 | php | wang |
+------+------+--------+
로그인 후 복사
同时,下面两个SQL也是等价的。
mysql> select * from table1 natural left join table2;
mysql> select * from table1 left join table2 using(id);
结果集:
+------+------+--------+
| id | book | author |
+------+------+--------+
| 1 | java | NULL |
| 2 | c++ | zhang |
| 3 | php | wang |
+------+------+--------+
로그인 후 복사
Left Join 左外连接 左外连接A、B表的意思就是将表A中的全部记录和表B中字段连接形成的记录集,这里注意的是最后出来的记录集会包括表A的全部记录。 左连接表1,表二等价于右连接表二,表一。如下两个SQL是等价的:
mysql> select * from table1 left join table2 using (id);
mysql> select * from table2 right join table1 using (id);
结果集:
+------+------+--------+
| id | book | author |
+------+------+--------+
| 1 | java | NULL |
| 2 | c++ | zhang |
| 3 | php | wang |
+------+------+--------+
로그인 후 복사
Right Join 右外连接
右外连接和左外连接是类似的。为了方便数据库便于访问,推荐使用左外连接代替右外连接。
最后,讲一下Mysql表连接的一些注意事项。
1、两个表求差集的方法 如果求 左表 - 右表 的差集,使用类似下面的SQL:
SELECT left_tbl.* FROM left_tbl LEFT JOIN right_tbl ON left_tbl.id = right_tbl.id WHERE right_tbl.id IS NULL;
例如
mysql> select table1.* from table1 left join table2 using(id) where table2.id is null;
+------+------+
| id | book |
+------+------+
| 1 | java |
+------+------+
1 row in set (0.00 sec)
mysql> select id, book, author from table1 join table2 using (id);
mysql> select table1.id, book, author from table1 join table2 on table1.id=table2.id;
结果集:
+------+------+--------+
| id | book | author |
+------+------+--------+
| 2 | c++ | zhang |
| 3 | php | wang |
+------+------+--------+
로그인 후 복사
但是下面两个有些许不同,使用on时候,重复的部分会被输出两次。
mysql> select * from table1 join table2 using (id);
+------+------+--------+
| id | book | author |
+------+------+--------+
| 2 | c++ | zhang |
| 3 | php | wang |
+------+------+--------+
2 rows in set (0.00 sec)
mysql> select * from table1 join table2 on table1.id=table2.id;
+------+------+------+--------+
| id | book | id | author |
+------+------+------+--------+
| 2 | c++ | 2 | zhang |
| 3 | php | 3 | wang |
+------+------+------+--------+
2 rows in set (0.00 sec)
SQL1 : SELECT * FROM t1, t2 JOIN t3 ON (t1.i1 = t3.i3);
SQL2 : SELECT * FROM (t1, t2) JOIN t3 ON (t1.i1 = t3.i3);
SQL3 : SELECT * FROM t1, (t2 JOIN t3 ON (t1.i1 = t3.i3));
SQL1 : SELECT ... FROM t1 NATURAL JOIN t2 NATURAL JOIN t3;
SQL2 : SELECT ... FROM t1, t2, t3 WHERE t1.b = t2.b AND t2.c = t3.c;
SQL3 : SELECT ... FROM t1, t2, t3 WHERE t1.b = t2.b AND t2.c = t3.c AND t1.a = t3.a;
로그인 후 복사
위 내용은 MySQL에서 Join 사용 사례 공유의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!