【发布时间】:2015-04-17 19:33:52
【问题描述】:
当我需要验证选择查询的返回是否为空或有值时,是否可以执行 SELECT CASE、解码、nvl 或其他查询功能? 例如,我有这个:
Record | type | id_customer
-------+--------+-------------
1 | T | cus1
2 | A | cus2
3 | T | cus3
4 | | cus4
如果我这样做:
select decode(type,'T','Main','A','Adicional','none') from table where record=1;
我得到主。
如果我这样做:
select decode(type,'T','Main','A','Adicional','none') from table where record=4;
我没有。
但如果我这样做:
select decode(type,'T','Main','A','Aditional','none') from table where record=5;
我什么也得不到,而且是合乎逻辑的。所以,我需要在行存在时获取解码值,如果行不存在,我需要获取文本。
所以,我尝试使用 SELECT CASE,但无法使用 COUNT 获取值。比如这样:
SELECT
CASE
WHEN count(1)>0 THEN decode(type,'T','Main','A','Aditional','none')
ELSE '-'
END
FROM TABLE WHERE record=5;
并得到一个'-',或者如果记录为2,则相同,得到'附加'
非常感谢。
【问题讨论】: