连接两个表、比较和更新的 SQL 语法是什么?

What is the SQL syntax for joining two tables, comparing and updating?

我正在尝试根据来自另一列的信息更新一个 table 中的单个列,但前提是它们不相同。下面代码的开头部分(直到 case)是我到目前为止所得到的,但我似乎无法在更新之前获得正确的语法来比较。

UPDATE table1
SET table.column1 = table2.column2
 FROM table1
inner join table2 ON
    table1.KEY = table2.KEY
    WHERE column4 = something and DATE between '10/12/14' and '10/15/14'
 CASE 
    WHEN table1.column1 != table2.column2
end;

我正在使用 SQL 服务器 2008r2,但任何 SQL 兼容的代码也很棒。谢谢

这是让我陷入循环的比较部分。加入 tables 不是直接更新。这就是为什么另一个问题不太正确。

关于有问题的代码,我认为您需要这样的东西:

UPDATE table1
SET table.column1 = table2.column2
FROM table1
inner join table2 ON table1.KEY = table2.KEY
           and table1.column4 = something 
           and table1.DATE between '10/12/14' and '10/15/14'
           and table1.column1 != table2.column2
UPDATE t1
SET t1.column1 = t2.column2
 FROM table1 t1
inner join table2 t2 ON t1.[KEY] = t2.[KEY]
WHERE column4 = 'something' 
AND [DATE] >= '20141012'   --<-- Avoid using between operator for dates
AND [DATE] <= '20141015'   --<-- Also use ASNI date format YYYYMMDD (Sargable)
AND t1.column1 <> t2.column2

您避免对日期值使用 Between 运算符的原因阅读此 What do BETWEEN and the devil have in common?