首页 > 代码库 > 【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)
- 外连接
- 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
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。