=*
*=
這是SQL7.0是方法在oracle可這樣用,MS SQL最好不要用,會造成數據不準確現象;聯機上有說明,曾測過語句多表連接時會出現
*=
這是SQL7.0是方法在oracle可這樣用,MS SQL最好不要用,會造成數據不準確現象;聯機上有說明,曾測過語句多表連接時會出現
解决方案 »
- SQL Server 2008写一个存储过程
- sql 基础问题 / 除
- 这个sql语句不会写,求救
- 求一sql
- 求段sql,考勤异常与请假资料匹配
- SQLSERVER2005服务进程CPU总是100%,怎么解决?另外在管理器中,竟显示有二个CPU,我可是只有一个啊
- 设置同步后,IMAGE字段最多能保存多少
- 我在SQL SERVER中写了个作业来定时备份数据库,必须要启动SQL SERVER Agent服务吗?启动了这个服务后跟平常的SQL SERBER 服务有什么区别
- 请问各位,我的存储过程参数怎么没有返回值呢.在分析器里面都有结果的,代码在里面,帮忙看看
- SqlServer7连接的问题,望各位仁兄帮忙
- 请教怎样在自定义函数中增减临时表字段的问题
- 难题:从XML列里提取值?SQL高手请进!
right join 右聯
create table t1(id int, feild int)
insert into t1 values(1 , 1)
insert into t1 values(1 , 2)
insert into t1 values(1 , 3)
insert into t1 values(1 , 4)
insert into t1 values(2 , 1)
insert into t1 values(2 , 2)
create table t2(id int, feild int)
insert into t2 values(1 , 1)
insert into t2 values(1 , 2)
insert into t2 values(1 , 5)
insert into t2 values(1 , 6)
insert into t2 values(2 , 1)
insert into t2 values(2 , 3)
goselect t1.*,t2.* from t1 left join t2 on t1.id=t2.id and t1.feild=1
/*
id feild id feild
----------- ----------- ----------- -----------
1 1 1 1
1 1 1 2
1 1 1 5
1 1 1 6
1 2 NULL NULL
1 3 NULL NULL
1 4 NULL NULL
2 1 2 1
2 1 2 3
2 2 NULL NULL(所影响的行数为 10 行)
*/select t1.*,t2.* from t1 left join t2 on t1.id=t2.id where t1.feild=1
/*
id feild id feild
----------- ----------- ----------- -----------
1 1 1 1
1 1 1 2
1 1 1 5
1 1 1 6
2 1 2 1
2 1 2 3(所影响的行数为 6 行)
*/select t1.*,t2.* from t1 left join t2 on t1.id=t2.id and t2.feild=1
/*
id feild id feild
----------- ----------- ----------- -----------
1 1 1 1
1 2 1 1
1 3 1 1
1 4 1 1
2 1 2 1
2 2 2 1(所影响的行数为 6 行)
*/select t1.*,t2.* from t1 left join t2 on t1.id=t2.id where t2.feild=1
/*
id feild id feild
----------- ----------- ----------- -----------
1 1 1 1
1 2 1 1
1 3 1 1
1 4 1 1
2 1 2 1
2 2 2 1(所影响的行数为 6 行)
*/drop table t1 , t2*= 的写法已经不常用了,建议楼主改成 left join
select t1.*,t2.* from t1 left join t2 on t1.id=t2.id and t1.feild=1
此句中的and 是作为一个连接条件存在的,即:只有t1.feild= 1 的数据才与 t2左连接 t1.feild 不等与1的数据不进行连接,所以他们对应的t2的结果全部为null
select t1.*,t2.* from t1 left join t2 on t1.id=t2.id where t1.feild=1
此句中的and是做为对连接后的结果进行筛选操作的,t1.feild <> 1的数据根本就不在结果集中显示!
至于t2.feild 条件放在where的前后结果都一样,如果把此处的left join 换成right join 结果很明显就不一样。
总的来说在使用left join 时and条件放在where之前的话是将此条件作为一个衡量第二表连接时的条件的一部分.
放在where 之后的话是对连接之后的表进行筛选.
另外如果是把 outer Join 换成 inner Join的话,条件放在什么地方都一样!
那就是SQL Server的BUG呢?
把源代码改成如下:
select a.userid
,b.userid,
a.subname as dept
,a.deptname
,a.subname
,a.subname as groupname
,a.code,a.[name]
,a.pydate
,b.userid as checktime
from uvw_nzlemployee a
left join (select distinct userid from checkinout where checktime between '2009-01-31' and '2009-01-31 23:59:59') b
on a.userID = b.userID
and a.userid=1617
and b.userid is null
and a.userid=1617
order by a.userid 现在如果把and b.userid is null 注销掉时应该数据量会增加的
再用临时表过滤此条件.
已经解决.