Hql或Sql书写过程中join和where表管理区别
1.学校期间学习表关联连接知道join on 连接,left join连接,inner join连接 。内连接、外连接、自连接、多表连接、交叉连接。
到了公司,发现公司的表连接都只有一种,而且还不是我们以前学习过的,就是用最简单的where实现,具体如下:
--join
select * from A a join B b on a.id=b.aid;
--where
select * from A a,B b where a.id=b.aid;
where 也可以实现表关联,为什么还要使用join连接,join的话是不是会增加服务器的压力,查询缓慢呢?
现假设两种表
table1:
id score
1 80
2 90
3 85
table2:
score name
80 AA
90 BB
90 CC
两条sql
select * from table1,table2 where table1.id = table2.id and table2.name = 'AA';
select * from table1 left join table2 on table.id = table2.id where table2.name = 'AA';
第一条sql过程
1、中间表
where条件中连接条件:
table1.id = table2.id
table1.id table1.score table2.score table2.name
1 80 80 AAA
2 90 90 BBB
2 90 90 CCC
2.再对中间表过滤
查询条件:
table2.name = 'AA'
table1.id table1.score table2.score table2.name
1 80 80 AAA
第二条sql过程
on条件:
table1.score = table2.score and table2.name = 'AA'
(条件不为真的话也会返回左表的记录)
table1.id table1.score table2.score table2.name
1 80 80 AAA
2 90 null null
3 85 null null
总结:
这是左连接。
where获取的是虚表连接,是join…on最基本的功能,如果需要完成比较复杂,或者数操作繁琐的功能,还是用join关键字比较合适。
原作者链接:
https://blog.csdn.net/zhaoyf7746/article/details/78044013