我有三個表如下:更新列基於3級表和2分的規則
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.name不應該存在於@TableA相同的ID。所以對於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
SQL Server 2012: JOIN 3 tables for a condition