2013-06-26 169 views
1

有兩種表,還有兩個列是他們怎樣寫SQL在以下情況下

之間

table1: 
id, date 

table2: 
id, date 
同樣的問題是,如何更新表2的ID,可以匹配的日期2表,這意味着,例如

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

感謝

回答

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. 
+0

抱歉,但設置t2.id = t2.id? – user782104

+1

@ user782104錯誤**':)'** – Luv

+0

如何添加一個條件,其中t2.date> xxx? – user782104