表1:属性有id(pk),target(需要更新的属性,值存在表2的valu属性中)
表2:属性有pid(外键,引用表1中的id),value(更新表1时候target需要的属性),state(当记录中的state=1时,就更新表1中的相应记录)请教这类问题该怎么解决啊 ?谢谢
表2:属性有pid(外键,引用表1中的id),value(更新表1时候target需要的属性),state(当记录中的state=1时,就更新表1中的相应记录)请教这类问题该怎么解决啊 ?谢谢
from table_2 b
where table_1.[id]=b.pid
and b.state=1
set value=target
from table1 inner join table2
on id=pid
where state=1--这个意思?
Update A
Set A.Target=(Case When B.State=1 then B.value else A.Target end)
From T1 A Inner Join T2 B
On T1.id=T2.Pid
(
[id] int primary key,
target varchar(20)
)
insert into 表1
select 1,'' union all
select 2,'' union all
select 3,'' union all
select 4,''Create table 表2
(
pid int primary key,
value varchar(20),
state bit
)
alter table 表2 add constraint FK_表2 foreign key(pid) references 表1([id])insert into 表2
select 1,'值1',1 union all
select 2,'值2',0 union all
select 3,'值3',1 union all
select 4,'值4',0update 表1 set target=t.value from 表2 t
where 表1.[id] = t.pid and t.state=1
select * from 表1
---------------
id target
1 值1
2
3 值3
4
---------------