先给出一个例子吧!
有两张相同的表A和B(表中数据有NULL),分别位于不同的数据库中,现在有两条数据插入B中,想把A和B的数据同步!原始表A和B结构如下:
CustomerNO CustomerName PhoneNo Address
001 张三 NULL CHINA
002 李四 5555 USA
003 Cat 4444 USA
004 Dog NULL NULL
现在插入表B的数据如下:
005 Cat NULL JAPAN
006 Tiger 1111 NULL我写了如下语句:
insert into A (select * from B where not exists(select * from A where A.CustomerNo=B.CustomerNo))但是没有结果,我考虑了几个可能的原因:
1.语句有问题;
2.因为数据中有NULL值.所以请大家帮忙指正!急!!!!!
有两张相同的表A和B(表中数据有NULL),分别位于不同的数据库中,现在有两条数据插入B中,想把A和B的数据同步!原始表A和B结构如下:
CustomerNO CustomerName PhoneNo Address
001 张三 NULL CHINA
002 李四 5555 USA
003 Cat 4444 USA
004 Dog NULL NULL
现在插入表B的数据如下:
005 Cat NULL JAPAN
006 Tiger 1111 NULL我写了如下语句:
insert into A (select * from B where not exists(select * from A where A.CustomerNo=B.CustomerNo))但是没有结果,我考虑了几个可能的原因:
1.语句有问题;
2.因为数据中有NULL值.所以请大家帮忙指正!急!!!!!
--try:insert into A
select * from B where b.CustomerNO not in (select CustomerNO from A);
执行上面的sql看看有没有数据先select * from B where customerno is not null and not exists(select * from A where A.CustomerNo=B.CustomerNo)
再执行上面的,看看有无数据
CustomerNo是主键,不可能有NULL
可能是我的问题!我没有把问题真正的提出来,现在更正如下:
A和B结构大致相同,但B中多了一个字段BirthDate,而在同步A时不要这个字段
表B:
CustomerNO CustomerName PhoneNo Address BirthDate
001 张三 NULL CHINA 1111111
002 李四 5555 USA 22333
003 Cat 4444 USA 444
004 Dog NULL NULL 3334表A:
CustomerNO CustomerName PhoneNo Address
001 张三 NULL CHINA
002 李四 5555 USA
003 Cat 4444 USA
004 Dog NULL NULL 现在插入表B的数据如下:
005 Cat NULL JAPAN 345435
006 Tiger 1111 NULL 33332还是同步表A(注:表A中没有BirthDate字段!)PS:给另位理解上带来的麻烦请见谅!!
select CustomerNO,CustomerName,PhoneNo,Address
from b
where not exists(select 1 from a
where a.CustomerNO=b.CustomerNO);
(CustomerNO, CustomerName, PhoneNo, Address)
select CustomerNO, CustomerName, PhoneNo, Address
from B
where b.CustomerNO not in (select CustomerNO from A);
你的方法可以!3Q!现在我想问的是如果我用not exists如何实现呢??
(CustomerNO, CustomerName, PhoneNo, Address)
select CustomerNO, CustomerName, PhoneNo, Address
from B
where not exists (select 1 from A where a.CustomerNO=b.CustomerNO);