sql学习 inner join,left join,right join

总结:

JOIN(或 INNER JOIN)只返回两个表中匹配的记录。

LEFT JOIN(或 LEFT OUTER JOIN)从左表返回所有的记录,即使右表中没有匹配。如果右表中没有匹配,则结果中右表的部分将是 NULL。

RIGHT JOIN(或 RIGHT OUTER JOIN)从右表返回所有的记录,即使左表中没有匹配。如果左表中没有匹配,则结果中左表的部分将是 NULL。

$ db2 "select * from liys.test1"

ID NAME


1 test_01

2 test_02

3 test_03

3 record(s) selected.

$ db2 "select * from liys.test2"

ID NAME


3 test_03

4 test_04

5 test_05

3 test_03_03

4 record(s) selected.

$ db2 "select t1.id ,t1.name ,t2.id, t2.name from liys.test1 t1 inner join liys.test2 t2 on t1.id=t2.id "

ID NAME ID NAME


3 test_03 3 test_03

3 test_03 3 test_03_03

2 record(s) selected.

$ db2 "select t1.id ,t1.name ,t2.id, t2.name from liys.test1 t1 left join liys.test2 t2 on t1.id=t2.id "

ID NAME ID NAME


3 test_03 3 test_03

3 test_03 3 test_03_03

2 test_02 - -

1 test_01 - -

4 record(s) selected.

$ db2 "select t1.id ,t1.name ,t2.id, t2.name from liys.test1 t1 right join liys.test2 t2 on t1.id=t2.id "

ID NAME ID NAME


3 test_03 3 test_03

3 test_03 3 test_03_03

    • 5 test_05
    • 4 test_04

4 record(s) selected.

相关推荐
暮色妖娆丶2 小时前
不过是吃了几年互联网红利罢了,我高估了自己
java·后端·面试
NE_STOP3 小时前
MyBatis-参数处理与查询结果映射
java
狂奔小菜鸡3 小时前
Day40 | Java中的ReadWriteLock读写锁
java·后端·java ee
IvorySQL4 小时前
PostgreSQL 技术日报 (3月5日)|规划器控制力升级,内核能力再进阶
数据库·postgresql·开源
SimonKing4 小时前
JetBrains 用户狂喜!这个 AI 插件让 IDE 原地进化成「智能编码助手」
java·后端·程序员
狂奔小菜鸡4 小时前
Day39 | Java中更灵活的锁ReentrantLock
java·后端·java ee
NE_STOP17 小时前
MyBatis-配置文件解读及MyBatis为何不用编写Mapper接口的实现类
java
数据组小组18 小时前
免费数据库管理工具深度横评:NineData 社区版、Bytebase 社区版、Archery,2026 年开发者该选哪个?
数据库·测试·数据库管理工具·数据复制·迁移工具·ninedata社区版·naivicat平替
后端AI实验室1 天前
用AI写代码,我差点把漏洞发上线:血泪总结的10个教训
java·ai