使用sql中另一个select的结果更新2列

时间:2014-10-05 14:36:39

标签: sql sql-server

enter image description here

enter image description here

我需要在表1中选择不同的“col1”并在table2中更新“col1”及其类“col1Class”

这是我写的代码,但它不起作用:

UPDATE testing 
SET col1 = a.col , Class = a.Class
from testing inner join 
( select distinct col1 , col1Class As col,class
from TestAll 
)a

3 个答案:

答案 0 :(得分:1)

 UPDATE B 
 SET B.CLASS=A.CLASS
 FROM TESTING B
 JOIN (SELECT DISTINCT COL1 AS COL,COL1CLASS AS CLASS FROM TESTALL)A
 ON B.COL1=A.COL 

答案 1 :(得分:0)

UPDATE testing 
SET col1 = A.col , Class = A.Class
FROM testing INNER JOIN 
(SELECT DISTINCT col1 , col1Class As col,class
from TestAll) 
as A 
ON testing.col1 = A.col AND testing.col1class = A.class

答案 2 :(得分:0)

;with cte(col1, colclass)
as
(
    select col1, colclass
    from table1
    group by col1,colclass
)

update t2
set col1 = c.col1, class = c.colClass
from table2 t2
inner join cte c on t2.col1 = c.col1

试试这个。