【问题标题】:How to write the SQL in the following scenario [duplicate]以下场景中如何编写SQL [重复]
【发布时间】:2013-06-23 10:21:07
【问题描述】:

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

table1:
id, date

table2:
id, date

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

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

谢谢

【问题讨论】:

    标签: sql sql-server


    【解决方案1】:
    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.
    

    【讨论】:

    • 抱歉,设置 t2.id = t2.id ?
    • @user782104 搞错了:)
    • 添加条件 t2.date > xxx 怎么样?
    • @user782104 请查看更新后的答案。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多