SQL中内连接和外连接区别

</pre>弄个例题,直观一点。两个表:<p></p><p><span style="color:rgb(51,51,51); font-family:'Microsoft Yahei'; font-size:14px; line-height:28px; white-space:pre-wrap">--表stu</span></p><p><span style="color:rgb(51,51,51); font-family:'Microsoft Yahei'; font-size:14px; line-height:28px; white-space:pre-wrap; background-color:rgb(102,102,204)">id name</span></p><p><span style="color:rgb(51,51,51); font-family:'Microsoft Yahei'; font-size:14px; line-height:28px; white-space:pre-wrap; background-color:rgb(102,102,204)">1, Jack</span></p><p><span style="color:rgb(51,51,51); font-family:'Microsoft Yahei'; font-size:14px; line-height:28px; white-space:pre-wrap; background-color:rgb(102,102,204)">2, Tom</span></p><p><span style="color:rgb(51,51,51); font-family:'Microsoft Yahei'; font-size:14px; line-height:28px; white-space:pre-wrap; background-color:rgb(102,102,204)">3, Kity</span></p><p><span style="color:rgb(51,51,51); font-family:'Microsoft Yahei'; font-size:14px; line-height:28px; white-space:pre-wrap; background-color:rgb(102,102,204)">4, nono</span></p><p><span style="color:rgb(51,51,51); font-family:'Microsoft Yahei'; font-size:14px; line-height:28px; white-space:pre-wrap">--表exam</span></p><p><span style="color:rgb(51,51,51); font-family:'Microsoft Yahei'; font-size:14px; line-height:28px; white-space:pre-wrap; background-color:rgb(102,51,255)">id grade</span></p><p><span style="color:rgb(51,51,51); font-family:'Microsoft Yahei'; font-size:14px; line-height:28px; white-space:pre-wrap; background-color:rgb(102,51,255)">1, 56</span></p><p><span style="color:rgb(51,51,51); font-family:'Microsoft Yahei'; font-size:14px; line-height:28px; white-space:pre-wrap; background-color:rgb(102,51,255)">2, 76</span></p><p><span style="color:rgb(51,51,51); font-family:'Microsoft Yahei'; font-size:14px; line-height:28px; white-space:pre-wrap; background-color:rgb(102,51,255)">11, 89</span></p><p><span style="color:rgb(51,51,51); font-family:'Microsoft Yahei'; font-size:14px; line-height:28px; white-space:pre-wrap">内连接 (显示两表id匹配的)</span></p><p><span style="color:rgb(51,51,51); font-family:'Microsoft Yahei'; font-size:14px; line-height:28px; white-space:pre-wrap"></span></p><pre name="code" class="sql">select stu.id,exam.id,stu.name, exam.grade from stu inner join exam on stu.id=exam.id
stu.id exam.id name grade

--------------------------------

1 1 Jack 56

2 2 Tom 76

左连接(显示join 左边的表的所有数据,exam只有两条记录,所以stu.id,grade 都用NULL 显示)

select stu.id,exam.id,stu.name, exam.grade from stu left join exam on stu.id=exam.id

1 1 Jack 56

2 2 Tom 76

3 NULL Kity NULL

4 NULL nono NULL

右连接(与作连接相反,显示join右边表的所有数据)

select stu.id,exam.id,stu.name, exam.grade from stu right join exam on stu.id=exam.id

1 1 Jack 56

2 Tom 76

NULL 11 NULL 89

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值