sql表连接
sql中表连接
分为内连接和外连接和全连接,外连接又分为左外连接和右外连接,还有个交叉连接
内连接
join或者inner join,内连接返回两个表中匹配的行,只有当连接条件满足时,才会返回结果
1 | select * from table1 join table2 on table1.id = table2.id; |
左外连接
left join,左外连接返回左表中的所有行,右表中符合连接条件的行,右表中不符合连接条件的行的值为NULL
1 | select * from table1 left join table2 on table1.id = table2.id |
右外连接
right join,右外连接返回右表中的所有行,左表中符合连接条件的行,左表中不符合条件的行的值为NULL
1 | select * from table1 right join table2 on table1.id = table2.id |
全连接
full join,全连接返回两个表中所有行,无论是否满足连接条件。某个表中没有匹配的行的值为NULL, 注意这个全连接mysql中不支持
1 | select * from table1 full join table2 on table1.id = table2.id |
交叉连接
cross join, 交叉连接又称笛卡尔积,返回结果是两个表中所有行的组合
1 | select * from table1 cross join table2; |
union连接
union是用于合并多个查询结果集的操作符,连接的结果包含所有查询的结果,并自动去除重复的行;union all表示所有查询的结果,也包括哪些重复的行
1 | select column1, column2 from table1 |
All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.
