两个表的内部查询和连接

时间:2013-11-20 16:42:28

标签: sql

我有一个ChildTable,我需要更新它的列。

从父表Id列中选择条件值。

Update ChildTable set Column1 = 'Value', Column2 = 'Value2'
Where ChildTable.Id = 100

2 个答案:

答案 0 :(得分:1)

尝试以下

Update ChildTable set Column1 = 'Value', Column2 = 'Value2' 
from ChildTable ct 
inner join parenttable pt on pt.key = ct.parentkey
Where ChildTable.Id = pt.parentconditionfield

答案 1 :(得分:0)

首先使用select并确保您获得正确的记录以进行更新。

因为你没有提供适当的信息,但是从你的例子中我得出了这个结论。

SELECT c_t.column1, c_t.column2
FROM   parent_table p_t inner join child_table c_t
ON     p_t.pk_column = c_t.fk_column
WHERE  c_t = 100;



UPDATE c_t
set    c_t.column1 = 'Value', c_t.column2 = 'Value2'
FROM   parent_table p_t inner join child_table c_t
ON     p_t.pk_column = c_t.fk_column
WHERE  c_t = 100;
相关问题