日期:2014-05-19  浏览次数:20757 次

sql数据库的问题100分 能得到结果就结贴给分
用a表更新b表         a表有(a1,a2,a3)列       列的内容如下;
a1       a2       a3
1         x1       y1
1         b1       c1
2         x1       y1
2         b1       c1
3         x3       y3
3         x1       y1
3         b1       c1
4         x4       y4
4         x3       y3
b表有(b1,b2,b3)列   内容如下
b1       b2       b3
1         x1       y1
1         b1       c1
2         x1       y1
2         b1       c1
20       x1       y1
所需要的结果是   ;插入时b表的b1列和a表的a1列内容相同时   不更新相同的行。
b1       b2       b3     .........更新后的b表列名
1         x1       y1   .........   b1列数据是1和a表a1内容相同   不更新
1         b1       c1   .........   b1列数据是1和a表a1内容相同   不更新
2         x1       y1   .........   b1列数据是2和a表a1内容相同   不更新
2         b1       c1          
20       x1       y1.........     a表里没有   就不管它
3         x3       y3   .........   a1列的数据是3   原b1没有         要更新          
3         x1       y1                     a表的a1列有3个3     要把3个行的内容
3         b1       c1                       都更新过来
4         x4       y4.........     a1列的数据是4   原b1没有       要更新
4         x3       y3                     把a1表的两个4的内容都更新过来

说明;那一堆汉字是解释;就是那a表更新b表。如果b表b1列里内容和a表a1列的内容相同就不更新那个一行。否则   就更新,   而且是多行的更新。如上面的   a1列的   3和4


------解决方案--------------------
其实就是一个insert语句吧
insert b select * from a where a1 not in (select distinct b1 from b)
------解决方案--------------------
create table a(a1 int,[a2] varchar(10),[a3] varchar(10))
insert into a select '1 ', 'x1 ', 'y1 '
insert into a select '1 ', 'b1 ', 'c1 '
insert into a select '2 ', 'x1 ', 'y1 '
insert into a select '2 ', 'b1 ', 'c1 '
insert into a select '3 ', 'x3 ', 'y3 '
insert into a select '3 ', 'x1 ', 'y1 '
insert into a select '3 ', 'b1 ', 'c1 '
insert into a select '4 ', 'x4 ', 'y4 '
insert into a select '4 ', 'x3 ', 'y3 '

create table b(b1 int,[b2] varchar(10),[b3] varchar(10))
insert into b select '1 ', '