MySQL学习之路6-数据表连接方式

内连接

关键字: inner join  on

语句:select * from <a_table> inner join <b_table> on a.id = b.id ;

说明:返回表之间关联字段相符的记录

Table:stuinfo

Table:stuscore

采用内连接查询方式:

select stuinfo.stuid,stu_name,gender,Math,English
from stuinfo inner join stuscore
on stuinfo.stuid = stuscore.stuid;

 查询结果如下:

左连接

关键字:left join  on

语句:select * from <a_table> left join <b_table> on a.id = b.id ;

说明:左表a_table的记录将全部显示出来,右表只会显示符合条件的记录,右表记录不足的地方将显示Null。

采用左连接的查询方式:

select stuinfo.stuid,stu_name,gender,Math,English
from stuinfo left join stuscore
on stuinfo.stuid = stuscore.stuid;

 查询结果如下:

右连接

关键字:right join  on

语句:select * from <a_table> right join <b_table> on a.id = b.id ;

说明:右表a_table的记录将全部显示出来,左表只会显示符合条件的记录,左表记录不足的地方将显示Null。

采用右连接的查询方式:

select stuscore.stuid,stu_name,gender,Math,English
from stuinfo right join stuscore
on stuinfo.stuid = stuscore.stuid;

查询结果如下:

2020-03-13 16:03

猜你喜欢

转载自www.cnblogs.com/fuyusheng/p/12469926.html