我有两张表 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”这条数据。
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”这条数据。
union all
select * from (
select * from tableA except select * from tableB) b
如果表a的数据 在表b中出现.此时取表b的.
只会修改driversize这个字段.
select * from tableA except select * from tableB) b
查不出值.
union all
select * from tableA a
where not exists(select 1 from tableB where serverid=a.serverid and driverid=a.driverid)
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
-----或者not exists,如果有其他的条件可以在加
select * from tableB
union all
(
select * from tableA where not exists (
select * from tableB where serverid=tableA.serverid)
)
union all
select * from tableA a
where not exists (select 1 from tableB b where a.serverid=b.serverid and a.driverid=b.driverid)
加一个自动增长列. 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谢谢了。因为牵扯到项目的变更与修改.
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)
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)
)
(
select *,ROW_NUMBER() OVER(PARTITION BY serverid ORDER BY GETDATE() DESC) as RN
from tableB
)
这个没看懂.
select serverid,driverid from tableA
except
select serverid,driverid from tableB
union all
select * from tableA a
where not exists(select 1 from tableB where serverid=a.serverid and driverid=a.driverid)