sql中join问题

来源:互联网 发布:软件试用报告模板 编辑:程序博客网 时间:2024/06/09 20:46
inner join 等价于 join 你可以理解为 join是inner join的缩写
left join 等价于 left outer join
right join 等价于 right outer join
A left join B 等效于 B right join A

inner join:理解为“有效连接”,两张表中都有的数据才会显示
left join:理解为“有左显示”,比如on a.field=b.field,则显示a表中存在的全部数据及a\\b中都有的数据,A中有、B没有的数据以null显示

select a.*,b.* from tableA a inner join tableB b on a.id=b.id (显性连接
等价于
select a.*,b.* from tableA a,tableB b
where a.id=b.id(隐性连接,随着数据库语言的规范和发展,已经逐渐被淘汰

left join:以左表为基准,根据on条件过滤连接生成临时表
on后面的过滤条件对左表无效,只对从表有效
例如:
select a.*,b.* from tableA a inner join tableB b on a.id=b.id and a.id=1
a.id=b.id and a.id=1这个条件只是作为左边关联右边数据的条件,而不是筛选左边数据的条件
left join会把左边的所有行都查出来