在工作中经常看见left join 的,我着来总结下
准备表:
test
test1
left join
- 左连接查询是以左表为基表,其中 on 是条件。有两张表或者多张表中有相同的字段才会查询出来。
例如:
select t.username ,t.pssword ,t2.u_id ,t2.name,t2.code
from test t left join test1 t2
on t.u_id =t2.u_id group by t.username order by t.u_id desc
这个sql查出来的结果是:
这个是test为主表,然后test1为测表。
right on
首先 ,右连接是以右边为基表,on 的条件不管有没有,都会以右连接的表为基表。
例如:
select t.username ,t.pssword ,t2.u_id ,t2.name,t2.code
from test t right join test1 t2 on
t.u_id =t2.u_id group by t.username order by t.u_id desc
这个sql查出来的结果:
这个是test1为主表,然后test为测表。
join on
- 首选连接,这个把关联的数据都连接。
例如:
select t.username ,t.pssword ,t2.u_id ,t2.name,t2.code
from test t join test1 t2 on
t.u_id =t2.u_id group by t.username order by t.u_id desc
例如:
这个是把关联的数据都展示了。
总结:
left join左连接,他是主表在左表,右表没有的数据会显示null;
right join右连接,他是主表在右表,左表没有的数据会显示null;
join on 全连接,他是连接你关联的表 on 后面跟条件
我的观点是用join on这个不会产生笛卡儿积,那俩个不会去重,会产生笛卡尔积,我也不知道明白,请网友知道的留言,学习学习。