主要是你没讲清楚原理和工程经验,或者面试官也是半瓶子醋。说明1:在组合查询中,left join 和 right join可能需要混合使用,这种情况下你应该反问面试官怎么颠倒呢?(当然也可以强行只用一个,但不符合软件维护性要求)在工程经验中,不是因为效果类似就能否定其作用。
说明2:原理。数据库执行过程解释下,就很容易明白 t1 left join t2 跟 t2 right join t1必须一样的。不一样就是数据库出问题了。
create table t1(id int primary key);
create table t2(id int primary key);
insert into t1 values(1),(2);
insert into t2 values(1),(3);
select * from t1
right join t2 on t1.id=t2.id;
select * from t2
left join t1 on t1.id=t2.id;
table1
id
1
2
table2
id
1
3
select * from table1 a
right join table2 b on a.id=b.id
这个结果是:
id, id
1, 1
select * from table1 a
left join table2 b on a.id=b.id
这个结果是:
id, id
1, 1
2, null
结果完全不一样