mysql 左连接_MySQL中的几种关联方式

MySQL中的集中关联方式

fd13c1514637499607de2653a2a869a1.png
  • 笛卡尔积
SELECT * FROM student_a a JOIN student_b b
  • join 与 inner join

在关联查询后面使用ON条件后,join 与 inner join查询的结果是一致的;

1. SELECT * FROM student_a a JOIN student_b b ON a.id = b.id2. SELECT * FROM student_a a INNER JOIN student_b b ON a.id = b.id
  • 左关联查询与右关联查询

左关联查询与右关联查询效果上是一直的:table_a a left join table_b b == table_b b right join table_a;

SELECT * FROM student_a a LEFT JOIN student_b b ON a.id = b.idSELECT * FROM student_b b LEFT JOIN student_a a ON b.id = a.idSELECT * FROM student_a a RIGHT JOIN student_b b ON a.id = b.idSELECT * FROM student_b b RIGHT JOIN student_a a ON b.id = a.id
  • 独有数据查询

查询一个数据表中特殊的数据:table_a - (table_a && table_b)

SELECT * FROM student_a a LEFT JOIN student_b b ON a.id = b.id WHERE b.id IS NULL SELECT * FROM student_a a RIGHT JOIN student_b b ON a.id = b.id WHERE a.id IS NULL
  • 全连接

Oracle数据库支持full join,mysql是不支持full join的,但仍然可以同过左外连接 + union + 右外连接实现

SELECT * FROM student_a a LEFT JOIN student_b b ON a.id = b.idUNION SELECT * FROM student_a a RIGHT JOIN student_b b ON a.id = b.id
  • 并集后去掉交集
SELECT * FROM student_a a LEFT JOIN student_b b ON a.id = b.id WHERE b.id IS NULL UNION SELECT * FROM student_a a RIGHT JOIN student_b b ON a.id = b.id WHERE a.id IS NULL
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值