将SELECT转换为更新语句

时间:2009-02-26 16:41:17

标签: sql sql-server

SELECT t1.status, t3.guid, t3.objectID
FROM Table1 t1, Table2 t2, Table3 t3
WHERE t2.ID = t3.ID
   AND t1.ID = t2.ID
   AND t3.Guid IN ('', '', '')

如何将此转换为我设置t1.status = 1

的更新语句

8 个答案:

答案 0 :(得分:6)

我首先将其转换为使用连接而不是“经典连接”:

select t1.status, t3.guid, t3.objectID
from Table1 t1
inner join Table2 t2 on t2.ID = t1.ID
inner join Table3 t3 on t3.ID = t2.ID
where t3.Guid in ('', '', '')

然后你可以直接翻录select语句并对其进行更新和设置语句:

update t1
set status = 1
from Table1 t1
inner join Table2 t2 on t2.ID = t1.ID
inner join Table3 t3 on t3.ID = t2.ID
where t3.Guid in ('', '', '')

答案 1 :(得分:2)

尝试这样的事情:

update Table1
set status = 1
from Table1 t1
    inner join Table2 t2 on t1.ID = t2.ID
    inner join Table3 t3 on t2.ID = t3.ID
where t3.Guid IN ('', '', '');

答案 2 :(得分:1)

UPDATE Table1
SET status = 1
FROM Table1 t1, Table2 t2, Table3 t3
WHERE t2.ID = t3.ID
   AND t1.ID = t2.ID
   AND t3.Guid IN ('', '', '')

答案 3 :(得分:0)

update Table1
set status = 1
FROM Table1 t1, Table2 t2, Table3 t3
WHERE t2.ID = t3.ID
   AND t1.ID = t2.ID
   AND t3.Guid IN ('', '', '')

答案 4 :(得分:0)

UPDATE Table1 set status = 1 
from table2 t2, table 3 t3 
where t2.ID = t3.ID 
and Table1.ID = t2.ID 
and t3.GUID in ('','','')

答案 5 :(得分:0)

我相信MS SQL Server(但不是Oracle),你可以写:

UPDATE TABLE1 
set t1.status = 1
FROM Table1 t1, Table2 t2, Table3 t3
WHERE t2.ID = t3.ID
   AND t1.ID = t2.ID
   AND t3.Guid IN ('', '', '')

答案 6 :(得分:0)

MERGE
INTO Table1 t1
USING
  (
  SELECT t2.id
  FROM Table2 t2, Table3 t3
  WHERE 
    t3.ID = t2.id
    AND t3.GUID IN ('', '', '')
 ) to
ON t1.id = to.id
WHEN MATCHED THEN
  UPDATE
  SET t1.status = 1

答案 7 :(得分:0)

update t1 set t1.status=(select 1 from Table1 t1, Table2 t2, Table3 t3 WHERE t2.ID = t3.ID   AND t1.ID = t2.ID  AND t3.Guid IN ('', '', ''))