如何从SQL Server的SELECT中进行UPDATE?

SQL Server中,可以使用 "SELECT "语句来 "插入 "一个表。

INSERT INTO Table (col1, col2, col3)
SELECT col1, col2, col3 
FROM other_table 
WHERE sql = 'cool'

是否也可以通过 "SELECT "进行_更新?我有一个包含数值的临时表,想用这些数值来更新另一个表。也许像这样。

UPDATE Table SET col1, col2
SELECT col1, col2 
FROM other_table 
WHERE sql = 'cool'
WHERE Table.id = other_table.id
解决办法
UPDATE
    Table_A
SET
    Table_A.col1 = Table_B.col1,
    Table_A.col2 = Table_B.col2
FROM
    Some_Table AS Table_A
    INNER JOIN Other_Table AS Table_B
        ON Table_A.id = Table_B.id
WHERE
    Table_A.col3 = 'cool'
评论(7)

在SQL Server 2008(或更高版本)中,使用MERGE

MERGE INTO YourTable T
   USING other_table S 
      ON T.id = S.id
         AND S.tsql = 'cool'
WHEN MATCHED THEN
   UPDATE 
      SET col1 = S.col1, 
          col2 = S.col2;

或者说:

MERGE INTO YourTable T
   USING (
          SELECT id, col1, col2 
            FROM other_table 
           WHERE tsql = 'cool'
         ) S
      ON T.id = S.id
WHEN MATCHED THEN
   UPDATE 
      SET col1 = S.col1, 
          col2 = S.col2;
评论(10)
UPDATE table 
SET Col1 = i.Col1, 
    Col2 = i.Col2 
FROM (
    SELECT ID, Col1, Col2 
    FROM other_table) i
WHERE 
    i.ID = table.ID
评论(4)

我想把罗宾的出色回答修改为以下内容。

UPDATE Table
SET Table.col1 = other_table.col1,
 Table.col2 = other_table.col2
FROM
    Table
INNER JOIN other_table ON Table.id = other_table.id
WHERE
    Table.col1 != other_table.col1
OR Table.col2 != other_table.col2
OR (
    other_table.col1 IS NOT NULL
    AND Table.col1 IS NULL
)
OR (
    other_table.col2 IS NOT NULL
    AND Table.col2 IS NULL
)

如果没有WHERE子句,你甚至会影响那些不需要被影响的记录,这(可能)会导致索引重新计算或触发那些真正不应该被触发的触发器。

评论(6)

单程

UPDATE t 
SET t.col1 = o.col1, 
    t.col2 = o.col2
FROM 
    other_table o 
  JOIN 
    t ON t.id = o.id
WHERE 
    o.sql = 'cool'
评论(0)

另一种尚未提及的可能性是将SELECT语句本身夹在CTE中,然后更新CTE。

;WITH CTE
     AS (SELECT T1.Col1,
                T2.Col1 AS _Col1,
                T1.Col2,
                T2.Col2 AS _Col2
         FROM   T1
                JOIN T2
                  ON T1.id = T2.id
         /*Where clause added to exclude rows that are the same in both tables
           Handles NULL values correctly*/
         WHERE EXISTS(SELECT T1.Col1,
                             T1.Col2
                       EXCEPT
                       SELECT T2.Col1,
                              T2.Col2))
UPDATE CTE
SET    Col1 = _Col1,
       Col2 = _Col2

这样做的好处是,可以很容易地先单独运行SELECT语句来对结果进行理智检查,但如果源表和目标表中的列的名称相同,则需要如上所述对它们进行别名。

这也和专有的UPDATE .... FROM语法一样的限制。 如果源表在一对多连接的多面,那么在 "Update "中就无法确定哪条可能匹配的连接记录将被使用(如果试图多次更新同一行,"MERGE "通过引发错误来避免这个问题)。

评论(3)

郑重声明(和其他像我一样搜索的人),你可以在MySQL中这样做。

UPDATE first_table, second_table
SET first_table.color = second_table.color
WHERE first_table.id = second_table.foreign_id
评论(0)

使用别名。

UPDATE t
   SET t.col1 = o.col1
  FROM table1 AS t
         INNER JOIN 
       table2 AS o 
         ON t.id = o.id
评论(0)

简单的方法就是。

UPDATE
    table_to_update,
    table_info
SET
    table_to_update.col1 = table_info.col1,
    table_to_update.col2 = table_info.col2

WHERE
    table_to_update.ID = table_info.ID
评论(3)

这可能是执行更新的一个小众理由(例如,主要用于存储过程),或者对其他人来说可能是显而易见的,但也应该说明,你可以在不使用join的情况下执行更新-选择语句(以防你'更新的表之间没有共同的字段)。

update
    Table
set
    Table.example = a.value
from
    TableExample a
where
    Table.field = *key value* -- finds the row in Table 
    AND a.field = *key value* -- finds the row in TableExample a
评论(0)

下面是另一种有用的语法。

UPDATE suppliers
SET supplier_name = (SELECT customers.name
                     FROM customers
                     WHERE customers.customer_id = suppliers.supplier_id)
WHERE EXISTS (SELECT customers.name
              FROM customers
              WHERE customers.customer_id = suppliers.supplier_id);

它通过使用"WHERE EXIST"检查是否为空。

评论(0)

我加这个只是为了让你看到一个快速的写法,以便你在做更新之前可以检查一下要更新的内容。

UPDATE Table 
SET  Table.col1 = other_table.col1,
     Table.col2 = other_table.col2 
--select Table.col1, other_table.col,Table.col2,other_table.col2, *   
FROM     Table 
INNER JOIN     other_table 
    ON     Table.id = other_table.id 
评论(0)

如果使用[MySQL][1]而不是SQL Server,则语法为:{{6623252}}}。

UPDATE Table1
INNER JOIN Table2
ON Table1.id = Table2.id
SET Table1.col1 = Table2.col1,
    Table1.col2 = Table2.col2

[1]: http://en.wikipedia.org/wiki/MySQL

评论(0)

在SQL数据库中用INNER JOIN从SELECT更新

由于这个帖子的回复太多,其中上票最多,我想在这里也提供一下我的建议。 虽然这个问题很有意思,但我在很多论坛网站上都看到了,并且用INNER JOIN做了一个解决方案,并附上截图。

一开始,我创建了一个名为schoolold的表,并根据列名插入了几条记录,然后执行它。

然后我执行SELECT命令来查看插入的记录。

[![][1]][1]

然后我创建了一个名为schoolnew的新表,并同样执行了上述操作。

[![][2]][2]

然后,为了查看其中插入的记录,我执行SELECT命令。

[![][3]][3]

现在,我想在第三行和第四行做一些修改,为了完成这个操作,我执行UPDATE命令和INNER JOIN

[![][4]][4]

为了查看变化,我执行SELECT命令。

[1]: http://i.stack.imgur.com/OdtDP.png [2]: http://i.stack.imgur.com/QP7Fw.png [3]: http://i.stack.imgur.com/BfqC5.png [4]: http://i.stack.imgur.com/agP7W.png 5:

你可以看到通过使用INNER JOIN和UPDATE语句,表schoolold的第三和第四条记录如何轻松地被表schoolnew替换。

评论(0)

下面的例子使用派生表,即在FROM子句后使用SELECT语句,返回旧值和新值,以便进一步更新。

UPDATE x
SET    x.col1 = x.newCol1,
       x.col2 = x.newCol2
FROM   (SELECT t.col1,
               t2.col1 AS newCol1,
               t.col2,
               t2.col2 AS newCol2
        FROM   [table] t
               JOIN other_table t2
                 ON t.ID = t2.ID) x
评论(0)

如果你想把表和自己连接起来(这不会太经常发生)。

update t1                    -- just reference table alias here
set t1.somevalue = t2.somevalue
from table1 t1               -- these rows will be the targets
inner join table1 t2         -- these rows will be used as source
on ..................        -- the join clause is whatever suits you
评论(1)

通过 "CTE "更新,比这里的其他答案更有可读性。

;WITH cte
     AS (SELECT col1,col2,id
         FROM   other_table
         WHERE  sql = 'cool')
UPDATE A
SET    A.col1 = B.col1,
       A.col2 = B.col2
FROM   table A
       INNER JOIN cte B
               ON A.id = B.id
评论(0)

如果你使用的是SQL Server,你可以从另一个表中更新一个表,而不需要指定连接,只需要从 "where "子句将两个表连接起来。 这使得SQL查询更加简单。

UPDATE Table1
SET Table1.col1 = Table2.col1,
    Table1.col2 = Table2.col2
FROM
    Table2
WHERE
    Table1.id = Table2.id
评论(0)

另一种方法是使用派生表。

UPDATE t
SET t.col1 = a.col1
    ,t.col2 = a.col2
FROM (
SELECT id, col1, col2 FROM @tbl2) a
INNER JOIN @tbl1 t ON t.id = a.id

样本数据

DECLARE @tbl1 TABLE (id INT, col1 VARCHAR(10), col2 VARCHAR(10))
DECLARE @tbl2 TABLE (id INT, col1 VARCHAR(10), col2 VARCHAR(10))

INSERT @tbl1 SELECT 1, 'a', 'b' UNION SELECT 2, 'b', 'c'

INSERT @tbl2 SELECT 1, '1', '2' UNION SELECT 2, '3', '4'

UPDATE t
SET t.col1 = a.col1
    ,t.col2 = a.col2
FROM (
SELECT id, col1, col2 FROM @tbl2) a
INNER JOIN @tbl1 t ON t.id = a.id

SELECT * FROM @tbl1
SELECT * FROM @tbl2
评论(0)
UPDATE TQ
SET TQ.IsProcessed = 1, TQ.TextName = 'bla bla bla'
FROM TableQueue TQ
INNER JOIN TableComment TC ON TC.ID = TQ.TCID
WHERE TQ.IsProcessed = 0

要确保你正在更新你想要的东西,首先选择

SELECT TQ.IsProcessed, 1 AS NewValue1, TQ.TextName, 'bla bla bla' AS NewValue2
FROM TableQueue TQ
INNER JOIN TableComment TC ON TC.ID = TQ.TCID
WHERE TQ.IsProcessed = 0
评论(0)