当前位置:网站首页>SQL联合查询(内联、左联、右联、全联)的语法
SQL联合查询(内联、左联、右联、全联)的语法
2022-06-23 18:50:00 【51CTO】
SQL联合查询(内联、左联、右联、全联)的语法
概述:
联合查询效率较高,举例子来说明联合查询:内联inner join 、左联left outer join 、右联right outer join 、全联full outer join 的好处及用法。
联合查询效率较高,以下例子来说明联合查询(内联、左联、右联、全联)的好处:
第一:内联(inner join)
如果想把用户信息、积分、等级都列出来,那么一般会这样写:select * from T1 ,T3 where T1.userid = T3.userid(其实这样的结果等同于select * from T1 inner join T3 on T1.userid=T3.userid )。
把两个表中都存在userid的行拼成一行(即内联),但后者的效率会比前者高很多,建议用后者(内联)的写法。
SQL语句:select * from T1 inner join T2 on T1.userid=T2.userid

第二:左联(left outer join)
显示左表T1中的所有行,并把右表T2中符合条件加到左表T1中;右表T2中不符合条件,就不用加入结果表中,并且NULL表示。
SQL语句:select * from T1 left outer join T2 on T1.userid=T2.userid

第三:右联(right outer join)
显示右表T2中的所有行,并把左表T1中符合条件加到右表T2中;左表T1中不符合条件,就不用加入结果表中,并且NULL表示。
SQL语句:select * from T1 right outer join T2 on T1.userid=T2.userid
第四:全联(full outer join)
显示左表T1、右表T2两边中的所有行,即把左联结果表+右联结果表组合在一起,然后过滤掉重复的。
SQL语句:select * from T1 full outer join T2 on T1.userid=T2.userid

总结
关于联合查询,效率的确比较高,4种联合方式如果可以灵活使用,基本上复杂的语句结构也会简单起来。这4种方式是:1)Inner join 2)left outer join 3)right outer join 4)full outer join
【版权声明】本博文著作权归作者所有,任何形式的转载都请联系作者获取授权并注明出处!
【重要说明】本文为本人的学习记录,论点和观点仅代表个人而不代表当时技术的真理,目的是自我学习和有幸成为可以向他人分享的经验,因此有错误会虚心接受改正,但不代表此刻博文无误!
【Gitee地址】秦浩铖: https://gitee.com/wjw1014
边栏推荐
- [one by one series] spa of identityserver4 (VI) authorization code process principle
- QGIS import WMS or WMTs
- 打新债有何要求 打新债安全吗
- 打新债好不好 打新债安全吗
- 盘点四种WiFi加密标准:WEP、WPA、WPA2、WPA3
- Activity registration | introduction to mongodb 5.0 sequential storage features
- Save: software analysis, verification and test platform
- Hardware development notes (6): basic process of hardware development, making a USB to RS232 module (5): creating USB package library and associating principle graphic devices
- Live sharing | Tencent cloud mongodb intelligent diagnosis and Performance Optimization Practice
- #19生成器函数经典案例
猜你喜欢

硬件开发笔记(六): 硬件开发基本流程,制作一个USB转RS232的模块(五):创建USB封装库并关联原理图元器件

LeetCode 1079. movable-type printing

The golden nine silver ten, depends on this detail, the offer obtains the soft hand!

活动报名 | MongoDB 5.0 时序存储特性介绍

20 provinces and cities announce the road map of the meta universe

Live sharing | Tencent cloud mongodb intelligent diagnosis and Performance Optimization Practice

JDBC 在性能测试中的应用

#19生成器函数经典案例

Development of block hash quiz game system (DAPP)

ElastricSearch第二弹之分片原理
随机推荐
墨天轮访谈 | IvorySQL王志斌—IvorySQL,一个基于PostgreSQL的兼容Oracle的开源数据库
20省市公布元宇宙路线图
Rendering of kotlin jetpack compose tab using animatedvisibility
Advanced network accounting notes (IV)
Online text entity extraction capability helps applications analyze massive text data
Live broadcast review | detailed explanation of koordinator architecture of cloud native hybrid system (complete ppt attached)
Convex optimization notes
Advanced network accounting notes (III)
火线沙龙第26期-多云安全专场
函數的定義和函數的參數
ElastricSearch第二弹之分片原理
Uniswap创始人:不会为Genie发行独立代币,Genie产品将集成至Uniswap界面
A review of comparative learning
【云动向】华为云云商店品牌全新发布 4大亮点都在这儿
Check four WiFi encryption standards: WEP, WPA, WPA2 and WPA3
混沌工程,了解一下
[cloud trends] the four highlights of Huawei cloud store brand new release are here
LeetCode 260. 只出现一次的数字 III
Netcf summary
Kubernetes 资源拓扑感知调度优化