这样的SQL语句该怎么写?表TABLE1,TABLE2,都有列w_name,bm ..... 并用相应行的TABLE2.bm替换TABLE1.bm
本帖最后由 my_yinger 于 2013-04-13 23:07:32 编辑
这样的SQL语句该怎么写?表TABLE1,TABLE2,都有列w_name,bm,dw,TABLE1大约有500条记录,TABLE2大概有4万条记录。两个表中一些行的w_name是相同的,如何找到TABLE2.w_name与TABLE1.w_name相同的记录,并用相应行的TABLE2.bm替换TABLE1.bm,TABLE2.dw替换TABLE1.dw
------解决方案--------------------;
WITH cte
AS ( SELECT a.w_name ,
a.bm ,
a.dw
FROM table2 a
INNER JOIN TABLE1 b ON a.w_name = b.w_name
)
UPDATE TABLE1
SET table1.bm = cte.bm ,
table1.dw = cte.dw
FROM TABLE1
INNER JOIN cte ON TABLE1.w_name = cte.w_name
------解决方案--------------------不用那么复杂,这句就可以了
UPDATE TABLE1
SET table1.bm = table2.bm ,
table1.dw = table2.dw
FROM TABLE1
INNER JOIN table2 ON TABLE1.w_name = table2.w_name
------解决方案--------------------难得遇到个简单的被大神抢先了