create table testA(
id int primary key,
name varchar(10)
)
insert into testA values(1,"小黄");
insert into testA values(2,"小红");
insert into testA values(3,"小蓝");
insert into testA values(4,"小绿");
insert into testA values(5,"小紫");
create table testB(
Id int primary key,
a_id int ,
age int
)
insert into testB values(1,1,10);
insert into testB values(2,2,11);
insert into testB values(3,2,12);
insert into testB values(4,2,13);
insert into testB values(5,3,13);
insert into testB values(6,4,13);
insert into testB values(7,4,13);
insert into testB values(8,2,13);
select a.id aid,a.name,b.id bid,b.a_id baid,b.age from testA a left join testB b on a.id=b.a_id
select a.id aid,a.name,b.id bid,b.a_id baid,b.age from testA a left join testB b on a.id=b.a_id and a.id=3
select a.id aid,a.name,b.id bid,b.a_id baid,b.age from testA a left join testB b on a.id=b.a_id where a.id=3
on条件是在生成临时表时使用的条件,它不管on中的条件是否为真,都会返回左边表中的记录。
where条件是在临时表生成好后,再对临时表进行过滤的条件。这时已经没有left join的含义(必须返回左边表的记录)了。
(1)此时相当于inner join on
(2)此时on后的条件用来生成左右表关联的临时表,where后的条件对临时表中的记录进行过滤。
无区别,不管是对左表还是右表进行筛选,on and 和 on where都会对生成的临时表进行过滤。