我有两张表 tableA与tableB 结果如下。
tableA:
serverid             driverid         driversize
服务器1             C                   1800
服务器2             C                   2000
服务器3             C                   5000
服务器4             C                   3000tableB:
serverid             driverid         driversize
服务器1             C                   2000
服务器2             C                   2000
服务器3             C                   5000此时我想用一条sql语句解决一个问题。我想要的结果如下:
例如表A中有条数据为:服务器1    C     1800
此时在表B中此条数据被修改成了:服务器1    C   2000
所以我取表B的数据,而不需要表A的数据。
但是如果表B中没有“服务器4”这条数据,所以我去表A中“服务器4”这条数据。

解决方案 »

  1.   

    select * from tableB
    union all
    select * from (
    select * from tableA except select * from tableB) b
      

  2.   

    表b中是存储表a修改后的数据.所以表b中的数据 对应到表a中其中的一条数据.
    如果表a的数据 在表b中出现.此时取表b的.
    只会修改driversize这个字段.
      

  3.   

    select * from (
    select * from tableA except select * from tableB) b
    查不出值.
      

  4.   

    select * from tableB
    union all
    select * from tableA a
    where not exists(select 1 from tableB where serverid=a.serverid and driverid=a.driverid)
      

  5.   

    /*
    tableA:
    serverid driverid driversize
    服务器1 C 1800
    服务器2 C 2000
    服务器3 C 5000
    服务器4 C 3000tableB:
    serverid driverid driversize
    服务器1 C 2000
    服务器2 C 2000
    服务器3 C 5000
    此时我想用一条sql语句解决一个问题。我想要的结果如下:
    例如表A中有条数据为:服务器1 C 1800
    此时在表B中此条数据被修改成了:服务器1 C 2000
    所以我取表B的数据,而不需要表A的数据。
    但是如果表B中没有“服务器4”这条数据,所以我去表A中“服务器4”这条数据。*/use City; 
    go 
    SET NOCOUNT ON 
    if OBJECT_ID(N'tableA',N'U') is not null drop table tableA 
    go 
    create table tableA 

    serverid nvarchar(50) not null, 
    driverid nvarchar(20), 
    driversize float 

    go 
    insert into tableA 
    select '服务器1','C', 1800 union all 
    select '服务器2','C',2000  union all 
    select '服务器3','C', 5000 union all 
    select '服务器4','C',3000 
    go 
    if OBJECT_ID(N'tableB',N'U') is not null drop table tableB 
    go 
    create table tableB 

    serverid nvarchar(50) not null, 
    driverid nvarchar(20), 
    driversize float 

    go 
    insert into tableB 
    select '服务器1','C', 2000 union all 
    select '服务器2','C',2000  union all 
    select '服务器3','C', 5000
    go select * from tableB 
    union all

     select * from tableA where serverid not in(select serverid from tableB)
    )
    ---结果如下
    serverid                                           driverid             driversize
    -------------------------------------------------- -------------------- ----------------------
    服务器1                                               C                    2000
    服务器2                                               C                    2000
    服务器3                                               C                    5000
    服务器4                                               C                    3000
      

  6.   


    -----或者not exists,如果有其他的条件可以在加
    select * from tableB 
    union all

     select * from tableA where not exists (
     select * from tableB where serverid=tableA.serverid)
    )
      

  7.   

    其实就是要 更新的数据(tableB) + 未更新的数据(tableA中未更新的记录),select * from tableB
    union all
    select * from tableA a 
    where not exists (select 1 from tableB b where a.serverid=b.serverid and a.driverid=b.driverid)
      

  8.   


    加一个自动增长列.  id .
    tableA:
    id serverid driverid driversize
    1 服务器1 C 1800
    2 服务器2 C 2000
    3 服务器3 C 5000
    4 服务器4 C 3000tableB:
    id serverid driverid driversize
    1 服务器1 C 2000
    2 服务器2 C 2000
    3 服务器3 C 5000
    4 服务器3 C 6000
    5 服务器2 C 7000谢谢了。因为牵扯到项目的变更与修改.
      

  9.   


    select * from tableB
    union all
    select * from tableA a 
    where not exists (select 1 from tableB b 
    where a.id=b.id and a.serverid=b.serverid and a.driverid=b.driverid)
      

  10.   

    /*
    tableA:
    serverid driverid driversize
    服务器1 C 1800
    服务器2 C 2000
    服务器3 C 5000
    服务器4 C 3000tableB:
    serverid driverid driversize
    服务器1 C 2000
    服务器2 C 2000
    服务器3 C 5000
    此时我想用一条sql语句解决一个问题。我想要的结果如下:
    例如表A中有条数据为:服务器1 C 1800
    此时在表B中此条数据被修改成了:服务器1 C 2000
    所以我取表B的数据,而不需要表A的数据。
    但是如果表B中没有“服务器4”这条数据,所以我去表A中“服务器4”这条数据。*/use City; 
    go 
    SET NOCOUNT ON 
    if OBJECT_ID(N'tableA',N'U') is not null drop table tableA 
    go 
    create table tableA 

    serverid nvarchar(50) not null, 
    driverid nvarchar(20), 
    driversize float 

    go 
    insert into tableA 
    select '服务器1','C', 1800 union all 
    select '服务器2','C',2000  union all 
    select '服务器3','C', 5000 union all 
    select '服务器4','C',3000 
    go 
    if OBJECT_ID(N'tableB',N'U') is not null drop table tableB 
    go 
    create table tableB 

    serverid nvarchar(50) not null, 
    driverid nvarchar(20), 
    driversize float 

    go 
    insert into tableB 
    select '服务器1','C', 2000 union all 
    select '服务器1','C', 2500 union all 
    select '服务器2','C',2000  union all 
    select '服务器3','C', 5000
    /*not in写行*/
    --select * from tableB 
    --union all
    --( 
    -- select * from tableA where serverid not in(select serverid from tableB)
    --)
    ;with cte as 
    (
       select *,ROW_NUMBER() OVER(PARTITION BY serverid ORDER BY GETDATE() DESC) as RN
       from tableB 
    )select serverid,driverid,driversize from (SELECT * FROM cte where RN=1)b
    union all

     select * from tableA where not exists (
     select * from tableB where serverid=tableA.serverid)
    )
      

  11.   

     如果有自动增长列的话,把 GETDATE()变成Id就可以了,楼主好好看看了
      

  12.   

    ;with cte as 
    (
       select *,ROW_NUMBER() OVER(PARTITION BY serverid ORDER BY GETDATE() DESC) as RN
       from tableB 
    )
    这个没看懂.
      

  13.   

    按照serverid 进行分组,按照GETDATE()进行排序
      

  14.   


    select serverid,driverid from tableA
    except
    select serverid,driverid from tableB
      

  15.   

    select * from tableB
    union all
    select * from tableA a
    where not exists(select 1 from tableB where serverid=a.serverid and driverid=a.driverid)