5

我正在尝试更新一列,同时将其加入另一个表。我使用了下面的查询,但它给了我错误。

UPDATE  TABLE_2 
INNER JOIN
      TABLE_1
ON     (T2.ID=T1.ID)
SET      TABLE_2.DEPT='HUMAN RESOURCE'
WHERE    TABLE_2.DEPT='HR'
AND T1.COMPANY =1 

谁可以帮我这个事?

谢谢。

4

3 回答 3

3

一方面,您正在使用未在任何地方定义的表别名(T2T1),这可能很好地解决您的问题。如果不是,那么正确的语法很大程度上取决于 SQL 风格。

例如,在 SQL Server 中,语法是

UPDATE T2
SET T2.dept = 'HUMAN RESOURCE'
FROM Table2 T2
   INNER JOIN Table1 T1
        ON T1.[ID] = T2.[ID]

虽然你甚至不需要在这里加入,但你只想要

UPDATE Table2 T2
SET T2.dept = 'HUMAN RESOURCE'
WHERE EXISTS(SELECT * FROM  Table1 T1
        ON T1.[ID] = T2.[ID])

在 MySQL 中,语法是

UPDATE FROM TABLE2 AS T2 
    INNER JOIN TABLE1 as T1 
        ON T2.id = T1.id 
SET T2.Dept = 'Human Resources' 

当然,该WHERE EXISTS方法也适用于 MySQL

UPDATE FROM Table2 AS T2
   SET Dept="Human Resources" 
   WHERE EXISTS (SELECT * FROM  Table1 T1
            ON T1.[ID] = T2.[ID]);
于 2012-05-31T04:17:54.613 回答
0

如果是 MSSQL,那么查询应该是

UPDATE TABLE_2 SET DEPT='Human Resource'
FROM TABLE_1
WHERE TABLE_2.ID = TABLE_1.ID
AND TABLE_2.DEPT = 'HR'
AND TABLE_1.COMPANY = 1
于 2012-05-31T04:25:07.213 回答
0

UPDATE TABLE_2 SET DEPT='Human Resource' FROM TABLE_1,Table2 WHERE TABLE_2.ID = TABLE_1.ID AND TABLE_2.DEPT = 'HR' AND TABLE_1.COMPANY = 1

因为当我们在加入时更新一个表时,我们从关闭中使用两个表

于 2014-03-19T15:27:18.170 回答