oracle的左连接和右连接 在oracle pl-sql中,左连接和右连接以如下方式来实现 查看如下语句: select emp_name,dept_name formemployee,department where employee.emp_deptid( + ) = department.deptid 此sql文使用了右连接,即(+)所在位置的另一侧为连接的
oracle的左连接和右连接
在oracle pl-sql中,左连接和右连接以如下方式来实现
查看如下语句:
select emp_name, dept_name
form employee, department
where employee.emp_deptid(+) = department.deptid
此sql文使用了右连接,即“(+)”所在位置的另一侧为连接的方向,右连接说明等号右侧的所有记录均会被显示,无论其在左侧是否得到匹配,也就是说上例中无论会不会出现某个部门没有一个员工的情况,这个部门的名字都会在查询结果中出现。
反之:
select emp_name, dept_name
form employee, department
where employee.emp_deptid = department.deptid(+)
则是左连接,无论这个员工有没有一个能在department表中得到匹配的部门号,这个员工的记录都会被显示
++++++++++++++++++++++++++++++++++++++++++mysql
a left join b 的连接的记录数与a表的记录数同
a right join b 的连接的记录数与b表的记录数同
a left join b 等价b right join a
table a:
field_k, field_a
1 a
3 b
4 c
table b:
field_k, field_b
1 x
2 y
4 z
select a.field_k, a.field_a, b.field_k, b.field_b
from a left join b on a.field_k=b.field_k
field_k field_a field_k field_b
---------- ---------- ---------- ----------
1 a 1 x
3 b null null
4 c 4 z
select a.field_k, a.field_a, b.field_k, b.field_b
from a right join b on a.field_k=b.field_k
field_k field_a field_k field_b
---------- ---------- ---------- ----------
1 a 1 x
null null 2 y
4 c 4 z
++++++++++++++++++++++++++++++++++++++
这样的。
table1 table2
id,sex1 id sex2
a 1 a 4
b 0
select id,sex1,sex2 from table1 left join table2 on table1.id=table2.id
则,
id sex1 sex2
a 1 4
b 0 null
也就是说left join 则连接左边表中所有记录都会出现,如果根据连接条件在table2中找不到相关记录,则显示为null
right join 则显示右边表中的全部记录。
inner join 则只有符合条件的记录才会出现在结果集中