在更新语法中使用带有子查询的内部联接

时间:2012-04-17 14:46:42

标签: sql sql-server

我正在尝试使用带有子查询的更新语句的内部联接...你能帮我解决sytax吗?还有你如何在sql server中使用AS子句作为别名??? 以下是我想要做的事情:

Update Table1 
inner join table2
set table1.value1 = (select table2.value1 where table1.value 1 ....)

任何想法?

4 个答案:

答案 0 :(得分:6)

如果需要使用子查询来执行UPDATE,可以这样做:

UPDATE t1
SET t1.value = t2.value
FROM Table1 t1
JOIN
(
    SELECT id, value
    FROM table2
) t2
ON t1.id = t2.id

答案 1 :(得分:2)

一种方法是对表进行别名:

update t1
set    table1.value1 = t2.value1
from   table1 as t1
join   table2 as t2
on     t1.id = t2.t1_id

答案 2 :(得分:1)

你应该试试

UPDATE table1 SET t1.value1 = t2.value2 
FROM table1 t1
INNER JOIN table2 t2 
    ON t1.field1 = t2.field2

答案 3 :(得分:0)

UPDATE Table1 t1
    INNER JOIN (
        SELECT id, value
        FROM table2
    ) t2 USING(id)
SET t1.value = t2.value