关于内连接,左连接(左外连接),右连接(由外连接)
左连接(左外连接) left join ··· on ··
select * from
table1 a
left join table2 b on a.id = b.id
left join table3 c on a.id = c.id
where a.id = '1';
左连接,查询左边表也就是table1 中所有符合a.id = ‘1’ ,table2,table3 中符合 a.id = b.id ,c.id = a.id 的数据
右连接即与左连接相反
内连接,查询 各个表的公共部门,即如果
(b.id = '1' || c.id = '1') == false
则查询结果为空
本次记录时遇到的问题: 联表查询三个表中id相同的数据,其中,table1 为主表,为了避免 table2 和table3 中id= 某值的数据为空时依然可以查出 table1 中的数据,因此,使用左连接
使用
select * from table1 a,table2 b,table3 c where a.id = b.id and a.id = c.id and a.id = #{id}
会导致 当table2 和table3 中 id = #{id} 的数据不存在时,查询结果为空