oracle中如何使用select和if条件?

发布于 2024-10-20 03:22:42 字数 279 浏览 6 评论 0原文

我的要求是从复杂的查询中获取一个数字并检查 num 是否=desiredNum。

如果它等于desiredNum,那么我必须执行另一组select语句,

有什么方法可以在查询中实现这一点而不是编写函数?

例如:

select case when val =2  
then select val1 from table1  
else 'false'  
from (select val from table)  

这可能吗?

My requirement is to get a number from a complex query and check if the num = desiredNum.

If it is equal to desiredNum, then I must perform another set of select statements,

Is there any way I can achieve this in a query rather than writing a function?

Eg:

select case when val =2  
then select val1 from table1  
else 'false'  
from (select val from table)  

Is this possible ??

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

懒猫 2024-10-27 03:22:42
select case when val=2 then val1 else val end as thevalue
from table1

我假设您的意思是 val 和 val1 都来自同一个表,但是当 val=2 时,要使用 val1 代替。如果您实际上有两个表,并且每个表都只有一条记录,那么

select
    case when val=2
    then (select val1 from table1)
    else 'false'
    end
from table
select case when val=2 then val1 else val end as thevalue
from table1

I assume you meant that val and val1 are both from the same table, but when val=2, to use val1 instead. If you actually had two tables, and they both have only one record each, then

select
    case when val=2
    then (select val1 from table1)
    else 'false'
    end
from table
日记撕了你也走了 2024-10-27 03:22:42

我不是 100% 我明白你需要什么。但我认为你可以使用联合来做到这一点:

create table theValues ( theValue integer)
create table table1 ( value1 integer)
create table table2 ( value2 integer)


INSERT INTO theValues (thevalue) VALUES (2)
INSERT INTO table1 ( value1 ) VALUES (17)
INSERT INTO table2 ( value2 ) VALUES (8)


SELECT value1 from table1 WHERE EXISTS (SELECT theValue from theValues WHERE theValue != 2)
UNION ALL 
SELECT value2 from table2 WHERE EXISTS (SELECT theValue from theValues WHERE theValue  = 2)

在这种情况下,“幻数”是2。如果theValues表查询返回2,那么你从表2的结果中得到结果,否则你从表1中得到结果 ,

干杯
丹尼尔

I am not 100% I understand what you need. But I think you could use a union to do this:

create table theValues ( theValue integer)
create table table1 ( value1 integer)
create table table2 ( value2 integer)


INSERT INTO theValues (thevalue) VALUES (2)
INSERT INTO table1 ( value1 ) VALUES (17)
INSERT INTO table2 ( value2 ) VALUES (8)


SELECT value1 from table1 WHERE EXISTS (SELECT theValue from theValues WHERE theValue != 2)
UNION ALL 
SELECT value2 from table2 WHERE EXISTS (SELECT theValue from theValues WHERE theValue  = 2)

In this case the "magic number" is 2. If the theValues table query returns 2, then you get the results from the result from table2, else you get the result from table 1.

Cheers,
Daniel

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文