SQL-Joins
SQL的连接方式
左外连接(left [outer] join ··· on)
select * from a1 left join a2 on a1.id = a2.id;
左表的数据全部输出,如果右表中无对应记录,字段为null。
内连接(inner [outer] join ··· on | join ··· on)
select * from a1 inner join a2 on a1.id = a2.id; select * from a1 join a2 on a1.id = a2.id;
输出的数据为左表和右表数据的交集。
右外连接(right [outer] join ··· on)
select * from a1 right join a2 on a1.id = a2.id;
右表的数据全部输出,如果左表中无对应记录,字段为null。
全外连接
(full join ··· on)Mysql不支持全连接,使用union连接左连接和右连接来实现全连接。
select * from a1 full join a2 on a1.id = a2.id; select * from a1 left join a2 on a1.id = a2.id union select * from a1 right join a2 on a1.id = a2.id;