简体   繁体   English

是或否是不使用IF或CASE的sql查询结果

[英]Yes or No as result of sql query without IF or CASE usage

Is there a way to make a query and return 'yes' or 'no' as an answer without the use of Flow Control operators? 有没有办法使用流控制运算符进行查询并返回“是”或“否”作为答案?

The only solution I thought is: 我认为唯一的解决方案是:

select 'yes' as answer 
from blabla
where blabla

but this of course works only if the query found some results. 但这当然仅在查询找到一些结果的情况下有效。

I like your blabla but don't understand your goal. 我喜欢你的blabla但不明白你的目标。

What is wrong with IF() statement or CASE WHEN ELSE END ? IF()语句或CASE WHEN ELSE END什么问题?

If you like your blabla code you can continue the same logic: 如果您喜欢blabla代码,则可以继续相同的逻辑:

select 'yes' as answer 
from blabla
where blabla.column=1
UNION
select 'no' as answer 
from blabla
where blabla.column<>1

and here is another tricky solution: 这是另一个棘手的解决方案:

http://sqlfiddle.com/#!9/76065/24 http://sqlfiddle.com/#!9/76065/24

SELECT @answer
FROM (
  SELECT @answer:='yes' 
  FROM blabla
  WHERE blabla.id=5
) y
RIGHT JOIN (SELECT @answer:='no') n
ON 1; 

You may want to COALESCE : 您可能需要COALESCE

select coalesce(
    select 'yes' as answer 
    from blablaT
    where blabla,
    select 'no' as answer 
    from blablaT
    where not blabla) as result from dual;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM