码迷,mamicode.com
首页 > 数据库 > 详细

【MYSQL】JOIN

时间:2017-01-04 00:24:29      阅读:254      评论:0      收藏:0      [点我收藏+]

标签:nan   分享   htm   div   hang   sel   strong   default   sch   

MYSQL

 

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

标签:nan   分享   htm   div   hang   sel   strong   default   sch   

原文地址:http://www.cnblogs.com/AlexBai326/p/6246764.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!