使用 Oracle 中同一行的值更新列

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9040301/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 00:34:38  来源:igfitidea点击:

Update a column using a value from the same row in Oracle

sqloracle

提问by Paul

I'm creating a SQL command in Oracle to update the value of "LOADDATE" to equal the value of "UPDATEDATE" on my table. What I have here works:

我正在 Oracle 中创建一个 SQL 命令来更新“LOADDATE”的值,使其等于我表上的“UPDATEDATE”的值。我在这里的作品:

BEGIN
  UPDATE LOAD_SETTINGS 
    SET 
      LOADDATE = (
        SELECT UPDATEDATE 
        FROM LOAD_SETTINGS
        WHERE
          MODEL = 'A001'
          AND OBJECT = 'A'
      )
    WHERE
      MODEL = 'A001'
      AND OBJECT = 'A';
  COMMIT;
END;

The one thing I know for sure about Oracle is that there is a lot that I do not know. Is there a better way to do this using any features of Oracle that I may not be familiar with? Perhaps without having to use a subquery? Or is this the the way to go?

关于 Oracle,我确定的一件事是,有很多我不知道的地方。有没有更好的方法来使用我可能不熟悉的 Oracle 的任何特性来做到这一点?也许不必使用子查询?或者这是要走的路?

Thanks!

谢谢!

回答by Conrad Frix

Since the where clause on the subquery is the same where on the outer there's no need for the subselect just reference the column directly

由于子查询上的 where 子句与外部的 where 子句相同,因此不需要子选择,只需直接引用列

UPDATE LOAD_SETTINGS 
SET 
  LOADDATE = UPDATEDATE 

WHERE
  MODEL = 'A001'
  AND OBJECT = 'A';

回答by Rajesh Kumar

UPDATE LOAD_SETTINGS 
SET    LOADDATE = UPDATEDATE 
WHERE  MODEL LIKE 'A001'
AND    OBJECT LIKE 'A';

Since Modeland Objectare varchar it is good to use LIKEinstead of =.

由于ModelObject是VARCHAR是很好用LIKE的,而不是=