oracle IF ELSE 语句并插入新列

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

IF ELSE Statement and Insert a new Column

sqloracle

提问by Futochan

I've been trying research online on how to combine "If" statements into my query. Suppose I wanted to create a query such that I want to create a extra column called "Description", and then include a if else statement such that "If Value = "A" then Description = "A", If Value = "B" then Description = "B", so on and so on. The problem is, since I have minimal access (not admin) to the databases. I can't create tables. I can only query the tables in oracles and export it out. Will that be an issue in terms of creating an extra column?

我一直在尝试在线研究如何将“If”语句组合到我的查询中。假设我想创建一个查询,以便创建一个名为“Description”的额外列,然后包含一个 if else 语句,使得“If Value = “A” then Description = “A”, If Value = “B”然后Description =“B”,依此类推。问题是,因为我对数据库的访问权限最小(不是管理员)。我无法创建表。我只能在oracles中查询表并将其导出。就创建额外的列而言,这会是一个问题吗?

Original:

原来的:

ID Value  
1  A  
2  B   
3  C

Want something like:

想要这样的东西:

ID Value Description(New Column)  
1  A     Apple  
2  B     Bacon  
3  C     Candy

Okay. I have no idea what I was doing below but it would be something like that? Where to I insert a new column called "Description"?

好的。我不知道我在下面做什么,但它会是那样的吗?在哪里插入一个名为“描述”的新列?

Select A.ID, B.Value  
From Table A  
Join Table B  
On A.ID = B.ID  
Where ID in ('1','2','3')  
If b.Value = 'A' then  
   (Description = "Apple")  
If b. value = 'B' then  
   (Description = "Bacon")
Group by A.ID, B.Value

回答by Barmar

You can use CASE:

您可以使用CASE

SELECT A.ID, B.Value,
       CASE B.Value
            WHEN 'A' THEN 'Apple'
            WHEN 'B' THEN 'Bacon'
            WHEN 'C' THEN 'Candy'
       END AS Description
FROM TableA A
JOIN TableB B ON A.ID = B.ID

回答by radar

you can do it like below

你可以像下面那样做

SELECT A.ID, A.Value, B.Description
FROM TABLEA A
JOIN ( SELECT 'A' as Value, 'Apple' as Description from dual
       UNION
       SELECT 'B' as Value, 'Bacon' as Description from dual
     ) T
on A.Value= B.Value

回答by Bacs

I'm not sure why you need to join the table to itself. Also, I presume you're not really trying to do this with a table named "table".

我不知道为什么你需要加入表格本身。另外,我认为您并没有真正尝试使用名为“table”的表来执行此操作。

CASE would do the job, as in Barmar's answer. I find DECODE more readable when the logic is this simple, but it's really a matter of taste. CASE is more flexible, which is not a matter of taste, but you don't need that flexibility here.

CASE 可以完成这项工作,就像 Barmar 的回答一样。当逻辑如此简单时,我发现 DECODE 更具可读性,但这确实是一个品味问题。CASE 更灵活,这不是品味的问题,但您不需要这里的灵活性。

select  id
       ,value
       ,decode( value
                 ,'A', 'Apple'
                 ,'B', 'Bacon'
                 ,'C', 'Candy' ) as Description
from table;