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.

相关推荐
QC班长10 分钟前
Maven公司私库配置踩坑点
java·服务器·maven·intellij-idea
Makoto_Kimur13 分钟前
java开发面试-AI Coding速成
java·开发语言
NoSi EFUL21 分钟前
MySQL中ON DUPLICATE KEY UPDATE的介绍与使用、批量更新、存在即更新不存在则插入
android·数据库·mysql
河阿里22 分钟前
SQL数据库:五大范式(NF)
数据库·sql·oracle
wuqingshun31415942 分钟前
说说mybatis的缓存机制
java·缓存·mybatis
空中海1 小时前
Kubernetes 生产实践、可观测性与扩展入门
java·贪心算法·kubernetes
Devin~Y1 小时前
大厂Java面试实录:Spring Boot/Cloud、Kafka、Redis、K8s 与 Spring AI(RAG/Agent)三轮连环问
java·spring boot·redis·mysql·spring cloud·kafka·kubernetes
l1t1 小时前
DeepSeek总结的PostgreSQL 19查询提示功能
数据库·postgresql
bLEd RING2 小时前
SpringBoot3.3.0集成Knife4j4.5.0实战
java
小松加哲2 小时前
Spring MVC 核心原理全解析
java·spring·mvc