where 后面如果仍是两个表中字段相等的条件,那不如直接在 on 后面加个 and 了. 结果没太大的区别,查询过程稍有不同.
on是连接条件 where 是过滤条件 不一样的。例如:declare @a table (id int,col varchar(1)) insert into @a select 1,'a' union all select 2,'b' union all select 3,'c' union all select 4,'d'declare @b table (id int,col varchar(1)) insert into @b select 1,'e' union all select 2,'f' union all select 3,'g'select * from @a a left join @b b on a.id=b.id and a.id>2 /* id col id col ----------- ---- ----------- ---- 1 a NULL NULL 2 b NULL NULL 3 c 3 g 4 d NULL NULL */select * from @a a left join @b b on a.id=b.id where a.id>2 /* id col id col ----------- ---- ----------- ---- 3 c 3 g 4 d NULL NULL */
对于INNER JOIN来说,写在ON和WHERE后效果是一样的
就你这一句,on和where的效果是一样的
玉,木有!!! 土疙瘩,有一块: 用执行计划检查了一下,在 on 后面用 and 和用 where ,搜索过程是一样的.
结果没太大的区别,查询过程稍有不同.
where 是过滤条件
不一样的。例如:declare @a table (id int,col varchar(1))
insert into @a
select 1,'a' union all
select 2,'b' union all
select 3,'c' union all
select 4,'d'declare @b table (id int,col varchar(1))
insert into @b
select 1,'e' union all
select 2,'f' union all
select 3,'g'select * from @a a left join @b b on a.id=b.id and a.id>2
/*
id col id col
----------- ---- ----------- ----
1 a NULL NULL
2 b NULL NULL
3 c 3 g
4 d NULL NULL
*/select * from @a a left join @b b on a.id=b.id where a.id>2
/*
id col id col
----------- ---- ----------- ----
3 c 3 g
4 d NULL NULL
*/
土疙瘩,有一块:
用执行计划检查了一下,在 on 后面用 and 和用 where ,搜索过程是一样的.