在数据库中新建三张表格:
T1
T2
T3
普通查询:
select * from T1, T3 where T1.user_id = T3.user_id
结果:
---------------------------------------------------------------
join就是把两张表格等效当做一张表来查
内联(inner join):
select * from T1 inner join T2 on T1.user_id = T2.user_id
on表示条件
T1 inner join T2表示T1和T2 整合起来当做一张表
结果:
---------------------------------------------------------------
左外联(left outer join):
select * from T1 left outer join T2 on T1.user_id=T2.user_id
T1 left outer join T2中的left表示左边先显示T1,T2要不要显示取决于on后面的条件
表示左边的表格不加限制
left outer join可以写成left join,所以左外联就是左联,下同
select * from T1 left join T2 on T1.user_id=T2.user_id
结果:
---------------------------------------------------------------
右外联(right outer join):
select * from T1 right outer join T2 on T1.user_id=T2.user_id
表示右边的表格不加限制
结果:
---------------------------------------------------------------
全外联(full outer join)
由于mysql不支持全联
select * from T1 left join T2 on T1.user_id = T2.user_id
union
select * from right join T2 on T1.user_id = T2.user_id ;
结果:
---------------------------------------------------------------
交叉联(cross join)也就是把两张表格的内容进行排列组合后返回
select * from T1 cross join T2
结果: