数据库的多表查询
案例:

1.结果:笛卡尔积查询:两张表相乘得出的结果

2.内连接查询:左边有且右边也有的记录:
select * from dept,emp where emp.dept_id=dept.id;或select * from dept inner join emp on emp.dept_id=dept.id;

3.左外连接查询:在内连接查询的基础上,加上左边表有而右边没有的记录:
select * from dept left join emp on emp.dept_id=dept.id;
4.右外连接查询:在内连接查询的基础上,加上右边有而左边表没有的记录:
select * from dept right join emp on emp.dept_id=dept.id;

5.全外连接查询:在内连接查询的基础上,加上左边表有而右边表没有的记录 和右边表有
而左边表没有的记录:
注意:select * from dept full join emp on emp.dept_id=dept.id; //mysql不支持
所以用下面方法:
select * from dept left join emp on emp.dept_id=dept.id
union
select * from dept right join emp on emp.dept_id=dept.id;


浙公网安备 33010602011771号