实验数据库:oracle
实验数据:
create table lilytestm ( id number(2), cloudname varchar2(20), colundtype varchar2(20));
create table lilytests ( id number(2), mid number(2), cloudcolor varchar2(20), colundtype varchar2(20));
insert into lilytestm values (1,'c1','t1');
insert into lilytestm values (2,'c2','t2');
insert into lilytestm values (3,'c3','t3');
insert into lilytests values (11,1,'red','t1');
insert into lilytests values (22,2,'green','t2');
SQL A:
select *
from lilytestm a
left join lilytests b
on a.id = b.mid
and a.cloudname = 'c2'
where colundtype = 't1'
SQLA 会报列错乱的错误。
SQL B:
select *
from lilytestm a
left join lilytests b
on a.id = b.mid
left join lilytests c
on a.id = c.mid
and a.cloudname = 'c2'
where colundtype = 't1'
SQLB不会报列错乱的错误。
在SQLB的结果中, 除了from的表有cloundtype字段外, 另外有两个left join的表中都有cloundtype字段,此时红色的cloundtype会选择最靠近它的表的列进行过滤,并且不错列错误错误。
为了避免上述错误的出现, 最好: 在使用left join的sql中, where 中的查询条件的字段带上表前缀。