多表连接时关于连接条件涉及到的Where 和And 问题
2010-11-10 15:34
204 查看
create table student
( [name] varchar(10),
class int
)
--往表Student中插入2条数据
insert into student values('Tom',1)
insert into student values('Jim', 2)
--建立score表
create table score
(
[name] varchar(10),
subject varchar(10),
scores int
)
--往表score中插入3条数据
insert into score values('Tom','Chinese', 100)
insert into score values('Tom','Math',99)
insert into score values('Jim','Math',80)
---选择两表数据
select * from student
select * from score
select a.[name],a.[class],b.[subject],b.[scores] from student a
left outer join score b on a.[name] = b.[name]
where (b.[subject] ='Chinese'or b.[subject] is null) -----此处使用的Where
select a.[name],a.[class],b.[subject],b.[scores] from student a left outer join score b on a.[name] = b.[name]
and (b.[subject] ='Chinese'or b.[subject] is null)
问题出现了吧 对于Where 和And 的使用不同 其结果就不同那么实质在哪里呢?CTRL+L 观察
对于Sql1-->首先执行 两个表的扫描,其次 执行嵌套查询Left Join 最后在利用筛选器执行Where 条件
对于Sql2-->执行两表查询,但是此处查询B表时却有了个条件限制B.id is Null 即在表B扫描时就把NULL的数据求出
--,然后是嵌套查询Left Join,此处没有筛选器(因为没有了Where 条件)
( [name] varchar(10),
class int
)
--往表Student中插入2条数据
insert into student values('Tom',1)
insert into student values('Jim', 2)
--建立score表
create table score
(
[name] varchar(10),
subject varchar(10),
scores int
)
--往表score中插入3条数据
insert into score values('Tom','Chinese', 100)
insert into score values('Tom','Math',99)
insert into score values('Jim','Math',80)
---选择两表数据
select * from student
select * from score
select a.[name],a.[class],b.[subject],b.[scores] from student a
left outer join score b on a.[name] = b.[name]
where (b.[subject] ='Chinese'or b.[subject] is null) -----此处使用的Where
select a.[name],a.[class],b.[subject],b.[scores] from student a left outer join score b on a.[name] = b.[name]
and (b.[subject] ='Chinese'or b.[subject] is null)
问题出现了吧 对于Where 和And 的使用不同 其结果就不同那么实质在哪里呢?CTRL+L 观察
对于Sql1-->首先执行 两个表的扫描,其次 执行嵌套查询Left Join 最后在利用筛选器执行Where 条件
对于Sql2-->执行两表查询,但是此处查询B表时却有了个条件限制B.id is Null 即在表B扫描时就把NULL的数据求出
--,然后是嵌套查询Left Join,此处没有筛选器(因为没有了Where 条件)
相关文章推荐
- sql 左连接查询条件and与where
- 关于MYSQL查询时WHERE条件值为数字的问题
- 关于MYSQL查询时WHERE条件值为数字的问题
- 关于php中使用odbc或者pdo连接sqlserver时如果查询条件中存在中文会导致查询失败的问题
- 关于在join中的on后面用where和and的有关问题
- 关于sql条件语句where id in (@参数)执行报错问题(转换成数据类型 int 时失败)(
- 关于数据库优化2——关于表的连接顺序,和where子句的前后顺序,是否会影响到sql的执行效率问题
- 关于linux服务器出现大量的Time_WAIT socket连接问题
- 关于“远程桌面连接已断开”问题的处理
- 关于mybatis中基本类型条件判断问题
- Android:关于Android Studio连接手机的各种问题 & 解决“通常每个套接字地址(协议/网络地址/端口)只允许使用一次(10048)”的问题。
- 关于ssh连接主机,git连接github失败的问题:ssh -T git@github.com&&Permission denied (publickey)
- 关于XMPP协议android客户端和openfire的连接问题
- 关于java的JDBC模式连接oracle问题
- 关于映射网络驱动 网络存在问题不能连接的原因
- 关于SQL2000无法远程连接的问题
- 关于Linux 双网卡,dubbo消费者出现连接不上的问题
- 关于Basic,Digest and NTLM Authentication认证的问题
- 关于thinkPHP中where中条件含变量时需要给变量加上单引号
- qt的关于编译条件的一些定义问题