更新表语法:查询在mysql中工作,但不在oracle或mssql中

时间:2017-02-01 17:58:29

标签: mysql sql sql-server oracle

update t_one t1 
join t_two t2 on (t2.f1 = t1.f1)
join t_three t3 on (t1.f4 = t3.f4 and t2.f5 = t3.f5) 
set t1.f6 = t3.f6 
where t1.f6 is null

我想用t_three中相应的值更新t_on.f6,该值取决于连接。查询在mysql中运行得很好,但是我得到了

ORACLE中的错误

  

ORA-00971:缺少SET关键字

和MSSQL

  

查询错误:'t1'附近的语法不正确

我该怎么办?

3 个答案:

答案 0 :(得分:2)

我永远不会记得各种条款出现在哪个顺序,但这是

的内容
update t1 
set t1.f6 = t3.f6 
from t_one t1
join t_two t2 on (t2.f1 = t1.f1)
join t_three t3 on (t1.f4 = t3.f4 and t2.f5 = t3.f5) 
where t1.f6 is null

您可能不需要t1set条款中的where别名,但它们可能不会受到伤害。

答案 1 :(得分:0)

对于SQL Server,您需要更多类似的东西:

UPDATE t1 
   SET t1.f6 = t3.f6 
  FROM t_one t1 
       INNER JOIN t_two t2 ON t2.f1 = t1.f1
       INNER JOIN t_three t3 ON t1.f4 = t3.f4 AND t2.f5 = t3.f5 
 WHERE t1.f6 is null

答案 2 :(得分:0)

在SQL Server中:

update  t1 
set t1.f6 = t3.f6 
from t_one t1
join t_two t2 on (t2.f1 = t1.f1)
join t_three t3 on (t1.f4 = t3.f4 and t2.f5 = t3.f5) 
where t1.f6 is null