跟燕十八学习PHP-第二十七天-左右内连接的区别
/**
燕十八 公益PHP培训
课堂地址:YY频道88354001
学习社区:www.zixue.it
**/
mysql> create table boy (
-> bname varchar(20),
-> other char(1)
-> )engine myisam charset utf8;
Query OK, 0 rows affected (0.23 sec)
mysql>
mysql> insert into boy
-> values
-> ('屌丝','A'),
-> ('李四','B'),
-> ('王五','C'),
-> ('高富帅','D'),
-> ('郑七','E');
Query OK, 5 rows affected (0.02 sec)
Records: 5 Duplicates: 0 Warnings: 0
mysql>
mysql>
mysql>
mysql> create table girl (
-> gname varchar(20),
-> other char(1)
-> )engine myisam charset utf8;
Query OK, 0 rows affected (0.16 sec)
mysql>
mysql> insert into girl
-> values
-> ('空姐','B'),
-> ('大S','C'),
-> ('阿娇','D'),
-> ('张柏芝','D'),
-> ('林黛玉','E'),
-> ('宝钗','F');
Query OK, 6 rows affected (0.03 sec)
Records: 6 Duplicates: 0 Warnings: 0
mysql> select * from boy;
+--------+-------+
| bname | other |
+--------+-------+
| 屌丝 | A |
| 李四 | B |
| 王五 | C |
| 高富帅 | D |
| 郑七 | E |
+--------+-------+
5 rows in set (0.00 sec)
mysql> select * from girl;
+--------+-------+
| gname | other |
+--------+-------+
| 空姐 | B |
| 大S | C |
| 阿娇 | D |
| 张柏芝 | D |
| 林黛玉 | E |
| 宝钗 | F |
+--------+-------+
6 rows in set (0.00 sec)
mysql> select boy.*,girl.* from
-> boy left join girl on boy.other=girl.other;
+--------+-------+--------+-------+
| bname | other | gname | other |
+--------+-------+--------+-------+
| 屌丝 | A | NULL | NULL |
| 李四 | B | 空姐 | B |
| 王五 | C | 大S | C |
| 高富帅 | D | 阿娇 | D |
| 高富帅 | D | 张柏芝 | D |
| 郑七 | E | 林黛玉 | E |
+--------+-------+--------+-------+
6 rows in set (0.00 sec)
mysql> #女生上台,带着另一半,没有另一半的,用NULL补齐
mysql> select boy.*,girl.* from
-> girl left join boy on boy.other=girl.other;
+--------+-------+--------+-------+
| bname | other | gname | other |
+--------+-------+--------+-------+
| 李四 | B | 空姐 | B |
| 王五 | C | 大S | C |
| 高富帅 | D | 阿娇 | D |
| 高富帅 | D | 张柏芝 | D |
| 郑七 | E | 林黛玉 | E |
| NULL | NULL | 宝钗 | F |
+--------+-------+--------+-------+
6 rows in set (0.00 sec)
mysql> #注意,a left join b,并不是说a表的就一定在左边,只是说在查询数据时,以a表为准
mysql> select * from boy;
+--------+-------+
| bname | other |
+--------+-------+
| 屌丝 | A |
| 李四 | B |
| 王五 | C |
| 高富帅 | D |
| 郑七 | E |
+--------+-------+
5 rows in set (0.00 sec)
mysql> select other,bname from boy;
+-------+--------+
| other | bname |
+-------+--------+
| A | 屌丝 |
| B | 李四 |
| C | 王五 |
| D | 高富帅 |
| E | 郑七 |
+-------+--------+
5 rows in set (0.00 sec)
mysql> #女生上台,带着另一半,没有另一半的,用NULL补齐
mysql> #不能用左连接来做
mysql> #用右连接. 刚才是 女生 left join 男
mysql> #用右连接,只需 男 right join 女
mysql> select boy.*,girl.*
-> from
-> boy right join girl
-> on boy.other = girl.other;
+--------+-------+--------+-------+
| bname | other | gname | other |
+--------+-------+--------+-------+
| 李四 | B | 空姐 | B |
| 王五 | C | 大S | C |
| 高富帅 | D | 阿娇 | D |
| 高富帅 | D | 张柏芝 | D |
| 郑七 | E | 林黛玉 | E |
| NULL | NULL | 宝钗 | F |
+--------+-------+--------+-------+
6 rows in set (0.01 sec)
mysql> select boy.*,girl.*
-> from
-> boy inner join girl
-> on boy.other=girl.other;
+--------+-------+--------+-------+
| bname | other | gname | other |
+--------+-------+--------+-------+
| 李四 | B | 空姐 | B |
| 王五 | C | 大S | C |
| 高富帅 | D | 阿娇 | D |
| 高富帅 | D | 张柏芝 | D |
| 郑七 | E | 林黛玉 | E |
+--------+-------+--------+-------+
5 rows in set (0.00 sec)
mysql> exit
燕十八老师太幽默了, 昨天的视频如下:
http://www.tudou.com/programs/view/TVhY20adVL4/