2014-03-31 68 views
0

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

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 

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

+0

您是否还可以将示例数据(更新前的内容以及更新后的内容)? – TTeeple

+0

@Teeple:table a abc def 0 0 -999 -999 table b abc def 1 1 -sa1283 -sa4958 table c -sa1283 1234 -sa4958 3456更新后的表A应该像这张表一样abc def 1 1 1234 3456希望这个将更清晰 - – user3375857

回答

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

+0

嗨,当我使用内部查询它是从表中的每一行返回重复行。 – user3375857

相关问题