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.

相关推荐
李兆龙的博客10 小时前
问津集 #26:Lakebase——Postgres 的版本化页面存储、数据库分支与计算弹性
数据库
Sarvartha10 小时前
final 关键字
java·开发语言
2601_9520477910 小时前
单一策略深度教程:用轻易云把集成任务的报错实时推送到钉钉机器人
java·机器人·钉钉
_upupup11 小时前
Linux的调试工具——gdb/cgdb
linux·服务器
数据狐(Datafox)11 小时前
淘宝图片搜索 API 落地实战:基于以图搜货搭建跨境电商选品系统
java·大数据·微服务
倔强的石头_12 小时前
聊聊金仓KFS:一款把数据同步软件做扎实的产品
数据库
闲云野鹤在人间12 小时前
MySQL|从理论、安装、备份到主从复制、MHA高可用详解
linux·运维·数据库·mysql·云计算
禾小西12 小时前
Redis:从两大维度和三大主线建立知识体系
数据库·redis·缓存