如何在以下场景中编写SQL

时间:2013-06-26 10:15:08

标签: sql sql-server

有两个表,它们之间也有两个列相同

table1:
id, date

table2:
id, date

问题是,如何更新表2的id,它可以匹配2表中的日期,也就是说,例如。

update table2 set table2.id = table1.id Where table1.date = table2.date

感谢

1 个答案:

答案 0 :(得分:3)

update t2 set t2.id = t1.id 
from table2 t2
inner join table1 t1 on t1.date = t2.date

<强> Edit

update t2 set t2.id = t1.id 
from table2 t2
inner join table1 t1 on t1.date = t2.date
where convert(date,t2.date)>'2013/01/01' --YYYY/MM/DD if your Date column is datetime.