select * from StudySQL.dbo.id_name n
inner join StudySQL.dbo.id_sex s
on n.id=s.id
and s.sex='f'
select * from StudySQL.dbo.id_name n
inner join StudySQL.dbo.id_sex s
on n.id=s.id
where s.sex='f'
结果是一样的。那么它们之间有什么区别吗?
添加
我做了几次更有趣的尝试。
select * from StudySQL.dbo.id_name n
1 | baby
3 | alice
select * from StudySQL.dbo.id_class c
1 | math
3 | physics
3 | english
4 | chinese
select * from StudySQL.dbo.id_name n
left join StudySQL.dbo.id_class c
on n.name='alice'
name id id class
baby 1 NULL NULL
alice 3 1 math
alice 3 3 physics
alice 3 3 english
alice 3 4 chinese
select * from StudySQL.dbo.id_name n
left join StudySQL.dbo.id_class c
on n.name='baby'
name id id class
baby 1 1 math
baby 1 3 physics
baby 1 3 english
baby 1 4 chinese
alice 3 NULL NULL
select * from StudySQL.dbo.id_name n
left join StudySQL.dbo.id_class c
on n.name<>''
name id id class
baby 1 1 math
baby 1 3 physics
baby 1 3 english
baby 1 4 chinese
alice 3 1 math
alice 3 3 physics
alice 3 3 english
alice 3 4 chinese
所以我认为可以合理地说,on 子句决定了哪些行应该被连接。而 where 子句决定应该返回哪些行。
如果这是真的,我认为最好在 on 子句中编写详细的限制,以便需要连接的行更少。因为连接是一项昂贵的操作。