tab1表
id A B num updatenum  1 1 3 55
2 1 4 77   
3 2 6 0
4 8 9 0  
5 10 11 99
6 13 14 0
tab2表
id A B num1 1 3 55
2 1 4 76
3 2 6 0
4 14 15 68
5 13 14 1id为主键(两个表中的a和b都是联合唯一) 两表连接条件为a,b以tab1为基准得到tab1表中如果在tab2中找不到 tab1.a=tab2.a and tab1.b=tab2.b 记录和 
找到tab1.a=tab2.a and tab1.b=tab2.b 但tab1.num<>tab2.num的记录
更新tab1表的 updatenum字段存放差值,tab1在tab2中找不到
tab1.a=tab2.a and tab1.b=tab2.b 条件的tab2.num 当成0来计算
用一条update语句能否写出来?
update后
select * from tab1 where not exists(select 1 from tab2 where tab1.a=tab2.a and tab1.b=tab2.b and tab1.num=tab2.num)
查询结果如下:
tab1
2 1 4 77 1
4 8 9 0  0
5 10 11 99 99
6 13 14 0 -1

解决方案 »

  1.   

    create table tab1(id int,A int,B int,num int,updatenum int)
    insert into tab1 values(1, 1 ,3, 55,null)
    insert into tab1 values(2, 1 ,4, 77,null)   
    insert into tab1 values(3, 2 ,6, 0,null)
    insert into tab1 values(4, 8 ,9, 0   ,null)
    insert into tab1 values(5, 10, 11, 99,null)
    insert into tab1 values(6, 13, 14, 0,null)
    create table tab2(id int,A int,B int,num int)
    insert into tab2 values(1 ,1 ,3 ,55)
    insert into tab2 values(2 ,1 ,4 ,76)
    insert into tab2 values(3 ,2 ,6 ,0)
    insert into tab2 values(4 ,14, 15, 68)
    insert into tab2 values(5 ,13, 14, 1)
    goupdate tab1
    set updatenum = num- isnull((select num from tab2 n where n.a = m.a and n.b = m.b),0)
    from tab1 mselect * from tab1
    /*
    id          A           B           num         updatenum   
    ----------- ----------- ----------- ----------- ----------- 
    1           1           3           55          0
    2           1           4           77          1
    3           2           6           0           0
    4           8           9           0           0
    5           10          11          99          99
    6           13          14          0           -1(所影响的行数为 6 行)
    */drop table tab1 , tab2
      

  2.   

    --> 测试数据:#1
    if object_id('tempdb.dbo.#1') is not null drop table #1
    create table #1(id int, A int, B int, num int, updatenum varchar(8))
    insert into #1
    select 1, 1, 3, 55,null union all
    select 2, 1, 4, 77,null union all
    select 3, 2, 6, 0,null union all
    select 4, 8, 9, 0,null union all
    select 5, 10, 11, 99,null union all
    select 6, 13, 14, 0,null
    --> 测试数据:#2
    if object_id('tempdb.dbo.#2') is not null drop table #2
    create table #2(id int, A int, B int, num int)
    insert into #2
    select 1, 1, 3, 55 union all
    select 2, 1, 4, 76 union all
    select 3, 2, 6, 0 union all
    select 4, 14, 15, 68 union all
    select 5, 13, 14, 1update a set a.updatenum = a.num - isnull(b.num,0)
    from #1 a left join #2 b on a.A=b.A and a.B=b.B
    where a.num<>b.num or b.num is nullselect * from #1/*
    id          A           B           num         updatenum
    ----------- ----------- ----------- ----------- ---------
    1           1           3           55          NULL
    2           1           4           77          1
    3           2           6           0           NULL
    4           8           9           0           0
    5           10          11          99          99
    6           13          14          0           -1
    */
      

  3.   

    这样不行啊,我那个在updatenum字段中更新的的是“错误差额为500”或无差额就更新成“Y”
    如果tab1的num为0 tab2中未找到对应的就也归为错误的一类更新为“错误差额为0”
      

  4.   

    开始不知道你还有这些需求。create table tab1(id int,A int,B int,num int,updatenum int)
    insert into tab1 values(1, 1 ,3, 55,null)
    insert into tab1 values(2, 1 ,4, 77,null)   
    insert into tab1 values(3, 2 ,6, 0,null)
    insert into tab1 values(4, 8 ,9, 0   ,null)
    insert into tab1 values(5, 10, 11, 99,null)
    insert into tab1 values(6, 13, 14, 0,null)
    create table tab2(id int,A int,B int,num int)
    insert into tab2 values(1 ,1 ,3 ,55)
    insert into tab2 values(2 ,1 ,4 ,76)
    insert into tab2 values(3 ,2 ,6 ,0)
    insert into tab2 values(4 ,14, 15, 68)
    insert into tab2 values(5 ,13, 14, 1)
    goupdate tab1 set updatenum = m.num - isnull(n.num,0)
    from tab1 m left join tab2 n
    on m.A = n.A and m.B = n.B
    where n.num is null or m.num<>n.num select * from tab1
    /*
    id          A           B           num         updatenum   
    ----------- ----------- ----------- ----------- ----------- 
    1           1           3           55          NULL
    2           1           4           77          1
    3           2           6           0           NULL
    4           8           9           0           0
    5           10          11          99          99
    6           13          14          0           -1(所影响的行数为 6 行)
    */drop table tab1 , tab2
      

  5.   


    挺好学习到2种方法 哈哈 也同样感谢SQLCenter