内连接,外连接,笛卡尔积

先创建两个表,一个学生表,一个成绩表。

 

 

一、内连接

概念:两个连接表的匹配行的连接

关键语句:table1 inner join table2 on

例子:

select * from Student inner join Score on Score.s_id=Student.s_id

这是进行内连接之后的表,展现出来的是共同的数据 (只返回匹配的行)

二、外连接(包括:左外连接,右外连接)

1) 左连接(左外连接)

概念:将返回右表的所有行。如果左表的某行在右表中没有匹配行,则将为右表返回空值

关键语句:table1 left  (outer) join table2 on

例子:

select * from Student  LEFT JOIN Score ON Student.s_id=Score.s_id
select * from Student left outer JOIN Score ON Student.s_id=Score.s_id

左表表,右表没数据为null

2) 右连接(右外连接)

概念:将返回右表的所有行。如果右表的某行在左表中没有匹配行,则将为左表返回空值;

关键语句:table1 right (outer) join table2 on

例子:

select * from Student right JOIN Score ON Student.s_id=Score.s_id
select * from Student right outer JOIN Score ON Student.s_id=Score.s_i

右表表,左表中没数据的为null

三、交叉连接(笛卡尔积)

概念:不带WHERE条件子句,它将会返回被连接的两个表的笛卡尔积,返回结果的行数等于两个表行数的乘积(例如:T_student和T_class,返回4*4=16条记录),如果带where,返回或显示的是匹配的行数。

关键语句:table1 cross join table2

例子:

select * from Student cross JOIN Score

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值