sql多表关联

student表,score表,

student的id字段和score的studentid字段关联。

student表中有1,2,而score表中有2,3。student表中有score表中没有的1,score表中有student表中没有的3.有一个交集是2。

drop table student;
create table student(
	id int not null primary key,
	name varchar(32)
);
drop table score;

create table score(
	studentid int,
	score int
);

insert into student(id,name) values (1,'wang');
insert into student(id,name) values (2,'liu');

insert into score(studentid,score) values(2,20);
insert into score(studentid,score) values(3,30);

内连接查询

select student.*,score.* from student,score where student.id = score.studentid;

查询结果:


左外连接

主表(左表)student中的数据逐条匹配表score中的数据
1、匹配,返回到结果集
2、无匹配,NULL值返回到结果集

select student.*,score.* from student left join score on student.id = score.studentid;

select score.*,student.* from score left join student on student.id = score.studentid;

右外连接

右表逐条去匹配记录;否则NULL填充
select student.*,score.* from student right join score on student.id = score.studentid;

select score.*,student.* from score right join student on student.id = score.studentid;



  • 2
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值