在 FROM 子句中指定外联接时,可以由下列几组关键字中的一组指定:
RIGHT JOIN 或 RIGHT OUTER JOIN。
右向外联接是左向外联接的反向联接。将返回右表的所有行。如果右表的某行在左表中没有匹配行,则将为左表返回空值。
完整外部联接返回左表和右表中的所有行。当某行在另一个表中没有匹配行时,则另一个表的选择列表列包含空值。如果表之间有匹配行,则整个结果集行包含基表的数据值。
例如:
表a
id name
1 a
2 b
3 c
表b
id store
1 15
2 10
4 67
select * from a inner join b on a.id=b.id
id name id store
1 a 1 15
2 b 2 10
select * from a inner join b where a.id=b.id
select * from a,b where a.id=b.id --连接查询的另一种写法
select * from a as e,b as r where e.id=r.id --使用as定义别名,当表名很长时有用
select * from a e,b r where e.id=r.id --定义别名时可以省掉as
select * from a,b where a.id=b.id --连接查询的另一种写法
select * from a as e,b as r where e.id=r.id --使用as定义别名,当表名很长时有用
select * from a e,b r where e.id=r.id --定义别名时可以省掉as
select * from a left outer join b on a.id=b.id
select * from a left join b on a.id=b.id
id name id stroe
1 a 1 15
2 b 2 10
3 c \N \N
select * from a right outer join b on a.id=b.id
select * from a right join b on a.id=b.id
id name id store
1 a 1 15
2 b 2 10
\N \N 4 67