MYSQLJOIN
Posted AlexBai326
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了MYSQLJOIN相关的知识,希望对你有一定的参考价值。
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)
- 文视图展示
以上是关于MYSQLJOIN的主要内容,如果未能解决你的问题,请参考以下文章