多表查询-外连接

外连接查询语法:
左外连接:select 字段列表 from 表1 left join 表2 on 连接条件...; 相当于查询表1的所有数据,以及表1和表2的交集部分数据

右外连接:select 字段列表 from 表1 right join 表2 on 连接条件...; 相当于查询表2的所有数据,以及表1和表2的交集部分数据(右外连接可用左外连接代替)

 

 

代码:

select emp.*,dept.name from emp left join dept on emp.dept_id = dept.id;/*左外连接:查询emp中所有数据,和对应的部门信息 */

select dept.*,emp.* from emp right join dept on emp.dept_id = dept.id;/*右外连接:查询dept中所有数据,和对应的员工信息 */
select dept.*,emp.* from dept left join emp on emp.dept_id = dept.id;/*将右外连接改为左外连接:查询dept中所有数据,和对应的员工信息 */
posted @ 2023-04-08 18:40  惊鸿宴远赴人间  阅读(62)  评论(0)    收藏  举报