1. Inner joins (内连接:同一数据库,表的连接)
- inner 可以省略
- 语法:join...表...on...条件...
- 给表名取个缩写更清晰
- 🌰 product_id 两张表都有,因此需要界定,同时作为连接
2. Join Across Database (跨数据连接:不同数据库,表的连接)
连接不在当前数据库的表,应在前面加上数据库名作为前缀
3. Self Joins (自连接)
🌰 单张表内自连接,看作两张表并分别命名
4. Join Multiple Tables (多表连接:大于2)
5.Compound Join Conditions(复合连接条件)
复合主键:有多个主键列,两个表存在共同特征
6.Implicit Join(隐式连接)
Attention:尽量不要使用隐式连接!!!(表述结构不够清晰)
7.Outer Join (外连接)
- left join :返回前表的所有数据,无论on条件是否匹配
- right join : 返回后表的所有数据,无论on条件是否匹配
上述🌰结果:上述表连接顺序为 products左,order_items右。没有对应order的product_id也会返回,对应oi_quantity为null
8. Other Join Between Multiple Tables (多表外连接)
Attention:此种情况尽量统一使用left join
🌰中:因为存在order,但还没有发货,即没有shipper_id的情况,因此连接shippers表用left join时表示,无shipper_id的也会返回,值返回null。
9. Self Outer Join(自外连接)
自连接+外连接(自连接也有null的情况)
10. The Using Clause (using 子句)
join...on... 的缩写
🌰 : using ( client_id) 等价于 on p.client_id = c.client_id
11. Natural Joins (自然连接)
数据库自己看着办,有不确定因素,不建议使用。
12. Cross Join(交叉连接)
cross连接的效果如下:
Attention : 被删掉的部分属于显式连接,推荐使用!!!
13.Unions (联合)
连接不同表的行,合并多个查询的结果
union是行连接
🌰结果: