有两个表tab1,tab2,两表结构完全相同.tab1和tab2的主键都为列s1,tab2的数据全是从tab1中抽出来的,现在要取出tab1中新增的数据,放到tab2中,示例:
tab1的数据:
s1 s2 s3
1 2 3
1 2 null
2 3 5
tab2的数据:
s1 s2 s3
1 2 null
1 2 4
现在想要得到的结果是:
2 3 5
用下面方法会得到含有null的记录
select * from tab1 where not exists(
select 1 from tab2 where tab1.t1 = tab2.t1 and tab1.t2=tab2.t2 and tab1.t3 = tab2.t3
)
会得到两行记录1 2 null
2 3 5 如何解决这个问题,请大侠们指点···
tab1的数据:
s1 s2 s3
1 2 3
1 2 null
2 3 5
tab2的数据:
s1 s2 s3
1 2 null
1 2 4
现在想要得到的结果是:
2 3 5
用下面方法会得到含有null的记录
select * from tab1 where not exists(
select 1 from tab2 where tab1.t1 = tab2.t1 and tab1.t2=tab2.t2 and tab1.t3 = tab2.t3
)
会得到两行记录1 2 null
2 3 5 如何解决这个问题,请大侠们指点···
解决方案 »
- 求一sql,在线等急用
- 救命啊,出事啦.老大要生气啦!!!!!!!!!!!!!!!!!解决不了就完啦
- SQL中的@变量有如何用
- 请教:服务器 'server_1' 上的 MSDTC 不可用。
- 数据统计问题
- 求SQL语句,包括函数运算,分组子句
- 关于取百分比的问题,取集合中的一定百分率数据的集合?
- windows优化大师卸载SQLserver时出现“停止服务“Eventlog”失败,请求的控件对此服务无效‘的消息框,该如何处理??
- 存储过程中大小写敏感问题 急
- 怎么用命令实现从远程服务器的SQLServer数据库数据与本地Access数据库数据的交互?
- AVG返回值类型的问题
- 求助,统计问题,谢谢先!!
select 1 from tab2 where tab1.t1 = tab2.t1 and tab1.t2=tab2.t2 and tab1.t3 = tab2.t3
)
and s3 is not null
select 1 from tab2 where
(tab1.t1 = tab2.t1 or (tab1.t1 IS NULL and tab2.t1 IS NULL)) and
(tab1.t2 = tab2.t2 or (tab1.t2 IS NULL and tab2.t2 IS NULL)) and
(tab1.t3 = tab2.t3 or (tab1.t3 IS NULL and tab2.t3 IS NULL))
)
如果字段比较多,就有可能涉及到有空值的字段了二楼和三楼的兄台:感谢你们提供的思路,但是方法也不是我需要的,在之前已经试过两位兄台给的方法了,谢谢!
select * from tt2 where not exists(
select 1 from tt3 where tt2.s1 = tt3.s1 and tt2.s2=tt3.s2 and COALESCE(tt2.s3,0) =
COALESCE(tt3.s3 ,0)
)
select * from tt2 where not exists(
select 1 from tt3 where tt2.s1 = tt3.s1 and tt2.s2=tt3.s2 and isnull(tt2.s3,0) =
isnull(tt3.s3 ,0)
)
如果字段比较多,有多个字段出现空值现象,这样解决不是一个最好的办法吧?
OR
直接设置字段不能为NULL值
SELECT * FROM tab1
WHERE NOT EXISTS (SELECT * FROM tab2 WHERE tab1.t1 = tab2.t1 AND CHECKSUM(tab1.t2,tab1.t3) = CHECKSUM(tab2.t2,tab2.t3))