首页 > 数据库 > MySQL > MySQL多表连接查询详解
内连接:在每个表中找出符合条件的共有记录
查询student表中的学生姓名和分数
第一种写法:只使用where
|
1 |
select
a.s_name, b.s_score
from
student a,score b
where
a.s_id = b.s_id; |
第二种写法:join … on…
|
1 |
select
a.s_name, b.s_score
from
student a
join
score b
on
a.s_id = b.s_id |
第三种写法:inner join … on…
|
1 |
select
a.s_name, b.s_score
from
student a
inner
join
score b
on
a.s_id = b.s_id |

左连接:根据左表的记录,在被连接的右表中找出符合条件的记录与之匹配,如果找不到与左表匹配的,用null表示
第一种写法:left join … on …
|
1 |
select
a.s_name,b.s_score
from
student a
left
join
score b
on
a.s_id = b.s_id |
第二种写法:left outer join … on …
|
1 |
select
a.s_name,b.s_score
from
student a
left
outer
join
score b
on
a.s_id = b.s_id |

**右连接:**根据右表的记录,在被连接的左表中找出符合条件的记录与之匹配,如果找不到匹配的,用null表示
第一种写法:right join … on …
|
1 |
select
a.s_name,b.s_score
from
student a
right
join
score b
on
a.s_id = b.s_id; |
第二种写法:right outer join … on …
|
1 |
select
a.s_name,b.s_score
from
student a
right
outer
join
score b
on
a.s_id = b.s_id; |

子查询:是多表连接查询的一种实现方式,在一个select语句的from子句或where子句中嵌套了另一个select语句,外层的select查询语句成为主查询,换句话将就是 WHERE或FORM中的查询语句称为子查询
WHERE子句中的子查询:子查询返回的值作为主查询的查询条件
FROM子句中的子查询:子查询返回的是一张虚拟的表,主查询从该查询从临时表查询出满足的条件
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/70014888/viewspace-2868383/ ,如需转载,请注明出处,否则将追究法律责任。