首页 > 代码库 > 【MYSQL】JOIN

【MYSQL】JOIN

MYSQL

使用的图片来自http://coolshell.cn/articles/3463.html

 

JOIN

mysql> 
mysql> desc College;
+-------+-------------+------+-----+---------+-------+
| Field | Type        | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| cname | varchar(50) | NO   | PRI | NULL    |       |
| state | varchar(20) | YES  |     | NULL    |       |
+-------+-------------+------+-----+---------+-------+
2 rows in set (0.00 sec)

mysql> desc Student;
+--------+-------------+------+-----+---------+-------+
| Field  | Type        | Null | Key | Default | Extra |
+--------+-------------+------+-----+---------+-------+
| sname  | varchar(50) | NO   | PRI | NULL    |       |
| school | varchar(50) | YES  |     | NULL    |       |
+--------+-------------+------+-----+---------+-------+
2 rows in set (0.00 sec)

mysql> select * from College;
+----------+----------+
| cname    | state    |
+----------+----------+
| swj      | sichuan  |
| nanchang | jiangsu  |
| nannong  | jiangsu  |
| beida    | beijing  |
| jiaoda   | shanghai |
+----------+----------+
5 rows in set (0.00 sec)

mysql> select * from Student;
+-------+----------+
| sname | school   |
+-------+----------+
| bai   | swj      |
| li    | nanchang |
| wang  | beida    |
| he    | masheng  |
| zhao  | zaodao   |
+-------+----------+
5 rows in set (0.00 sec)

mysql> 
  • 内联结

技术分享

mysql> SELECT * from College JOIN  Student on College.cname = Student.school;
+----------+---------+-------+----------+
| cname    | state   | sname | school   |
+----------+---------+-------+----------+
| swj      | sichuan | bai   | swj      |
| nanchang | jiangsu | li    | nanchang |
| beida    | beijing | wang  | beida    |
+----------+---------+-------+----------+
3 rows in set (0.00 sec)

mysql> SELECT * from College INNER JOIN  Student on College.cname = Student.school;
+----------+---------+-------+----------+
| cname    | state   | sname | school   |
+----------+---------+-------+----------+
| swj      | sichuan | bai   | swj      |
| nanchang | jiangsu | li    | nanchang |
| beida    | beijing | wang  | beida    |
+----------+---------+-------+----------+
3 rows in set (0.00 sec)

mysql> select * from College,Student  where College.cname = Student.school;
+----------+---------+-------+----------+
| cname    | state   | sname | school   |
+----------+---------+-------+----------+
| swj      | sichuan | bai   | swj      |
| nanchang | jiangsu | li    | nanchang |
| beida    | beijing | wang  | beida    |
+----------+---------+-------+----------+
3 rows in set (0.00 sec)

mysql> SELECT * from College  STRAIGHT_JOIN  Student on College.cname = Student.school;
+----------+---------+-------+----------+
| cname    | state   | sname | school   |
+----------+---------+-------+----------+
| swj      | sichuan | bai   | swj      |
| nanchang | jiangsu | li    | nanchang |
| beida    | beijing | wang  | beida    |
+----------+---------+-------+----------+
3 rows in set (0.00 sec)

 

  • 外连接
  1. left join

技术分享

mysql> SELECT * from College LEFT JOIN  Student on College.cname = Student.school;
+----------+----------+-------+----------+
| cname    | state    | sname | school   |
+----------+----------+-------+----------+
| swj      | sichuan  | bai   | swj      |
| nanchang | jiangsu  | li    | nanchang |
| nannong  | jiangsu  | NULL  | NULL     |
| beida    | beijing  | wang  | beida    |
| jiaoda   | shanghai | NULL  | NULL     |
+----------+----------+-------+----------+
5 rows in set (0.00 sec)

     2. left join where 

 技术分享

mysql> SELECT * from College LEFT JOIN  Student on College.cname = Student.school where Student.sname is NULL;
+---------+----------+-------+--------+
| cname   | state    | sname | school |
+---------+----------+-------+--------+
| nannong | jiangsu  | NULL  | NULL   |
| jiaoda  | shanghai | NULL  | NULL   |
+---------+----------+-------+--------+
2 rows in set (0.00 sec)

 

     2.right join

mysql> SELECT * from College RIGHT JOIN  Student on College.cname = Student.school;
+----------+---------+-------+----------+
| cname    | state   | sname | school   |
+----------+---------+-------+----------+
| swj      | sichuan | bai   | swj      |
| nanchang | jiangsu | li    | nanchang |
| beida    | beijing | wang  | beida    |
| NULL     | NULL    | he    | masheng  |
| NULL     | NULL    | zhao  | zaodao   |
+----------+---------+-------+----------+
5 rows in set (0.00 sec)

 3.  模拟full out join

技术分享

mysql>  SELECT * from College LEFT JOIN  Student on College.cname = Student.school union all SELECT * from College RIGHT JOIN  Student on College.cname = Student.school;
+----------+----------+-------+----------+
| cname    | state    | sname | school   |
+----------+----------+-------+----------+
| swj      | sichuan  | bai   | swj      |
| nanchang | jiangsu  | li    | nanchang |
| nannong  | jiangsu  | NULL  | NULL     |
| beida    | beijing  | wang  | beida    |
| jiaoda   | shanghai | NULL  | NULL     |
| swj      | sichuan  | bai   | swj      |
| nanchang | jiangsu  | li    | nanchang |
| beida    | beijing  | wang  | beida    |
| NULL     | NULL     | he    | masheng  |
| NULL     | NULL     | zhao  | zaodao   |
| NULL     | NULL     | nu    | NULL     |
+----------+----------+-------+----------+
11 rows in set (0.00 sec)

 

【MYSQL】JOIN