使用表b中的列更新表格列(表b中2列的值需要从表c中获取)

时间:2014-03-31 12:59:44

标签: sql-server

我需要通过表b列col3,col4,col5和col6更新表A列col3,col4,col5和col6,但是表b col5和col6值需要来自表c col1。 意味着表b col5和col6中有值,但是我需要用表c col1中的值替换它们,并且需要相应地更新表col5和col6。 表a和表b共有col1和col2。我正在尝试这样的事情。

Update a
a.col3 = b.col3,  
a.col4 = b.col4,
a.col5 = (select col1 from table_c c where c.col2=b.col5),
a.col6 = (select col1 from table_c c where c.col2=b.col6)
from table_A a inner join table_b
on  a.col1=b.col1 and a.col2=b.col2

有人可以帮我重构上面的更新查询吗? 提前感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

Update a a.col3 = b.col3, a.col4 = b.col4, a.col5 = c.col1, a.col6 = d.col1 from table_A a inner join table_b on a.col1=b.col1 and a.col2=b.col2 inner join table_c c on c.col2 = b.col5 inner join table_c d on d.col2 = b.col6

否则,在子查询中使用select top 1

相关问题