oracle 根据某些条件修改列值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23253124/
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 02:20:22  来源:igfitidea点击:

Modify column value based on some conditions

sqloracleoracle11g

提问by GoldfishGrenade

Given the following table:

鉴于下表:

 ... |  item  | type | ...
 ... |    A   |  1   | ...
 ... |    B   |  2   | ...
 ... |    A   |  2   | ...
 ... |    A   |  3   | ...
 ... |    B   |  3   | ...
 ... |    A   |  2   | ...

Let's call items by pairings (A1, B2, A2, etc). There is an error in the table where an A3 should actually be C3.

让我们通过配对(A1、B2、A2 等)来调用项目。表中有一个错误,其中 A3 实际上应该是 C3。

I want to write a query that will return rows and change the item column to C if the row returned A3.

我想编写一个查询,如果行返回 A3,它将返回行并将项目列更改为 C。

Example query that doesn't work:

不起作用的示例查询:

SELECT (item like 'A' ? (type == 3? 'C' : 'A') : item) as "item", type from table; 

My desired output would be:

我想要的输出是:

"item" | type
   A   |  1  
   B   |  2  
   A   |  2   
   C   |  3  
   B   |  3   
   A   |  2   

What is the correct way of doing this? If possible?

这样做的正确方法是什么?如果可能的话?

回答by Olesya Razuvayevskaya

   update table tablename
     set item='C'
     where type=3 and item='A'

If you don't need updating these values in the table, but to get replaced values them as a result of the select, then

如果您不需要更新表中的这些值,而是将它们作为选择的结果替换值,则

   select 
      case when item='A' and type=3 then 'C' 
           else item 
           end as newitem, type from tablename