我有三个表如下:
declare @tableA table (id int, name varchar(2));
declare @tableB table (name varchar(2));
declare @tableC table (id int, name varchar(2))
insert into @tableA(id, name)
select 01, 'A4' union all
select 01, 'SH' union all
select 01, '9K' union all
select 02, 'M1' union all
select 02, 'L4' union all
select 03, '2G' union all
select 03, '99';
insert into @tableB(name)
select '5G' union all
select 'U8' union all
select '02' union all
select '45' union all
select '23' union all
select 'J7' union all
select '99' union all
select '9F' union all
select 'A4' union all
select 'H2';
insert into @tableC(id)
select 01 union all
select 01 union all
select 01 union all
select 02 union all
select 02 union all
select 03 union all
select 03;
基本上,@TableC.ID 是从 @TableA.ID 填充的(相同的行)
现在,我必须考虑以下规则来填充@tableC.Name:
它应该从@TableB.name 获取值,前提是@TableA 中的相同ID 不应该存在相同的@TableA.name。所以对于 ID = 1,@TableC.name 应该是除 A4、SH、9K 之外的任何值。
@tableC.Name 对于每个 @TableC.ID 应该是 DISTINCT。所以@TableC.Name 不应该有两个相同的 ID 相同的值,但它可以有不同的 ID 相同的 @TableC.name。
我用来解决规则#1 的查询是:(请编辑它以应用规则#2)
update c
set Name = (select top 1 b.name
from @TableB b
where b.name not in (select name from @TableA a where a.id = c.id)
order by NEWID()
)
from @tableC c
select *
from @tableC