Oracle 11g: Default to static value when query returns nothing(Oracle 11g:当查询不返回任何内容时默认为静态值)
问题描述
在 Oracle 11g 中工作时,我需要选择一个与表中存在的输入值相对应的数据,而当该值存在于表中时,我需要选择一个静态默认值.我能找到的最好的方法是写这样的东西:
Working in Oracle 11g, I have a need to select a datum corresponding to an input value when that value exists in a table, and to instead select a static default value when it does not. The best way I could find to accomplish this was to write something like this:
SELECT desired_datum
FROM (
--Try to get explicit datum
SELECT desired_datum, 1 AS was_found
FROM data_table
WHERE the_key = &input_value
UNION
--Get default datum
SELECT 'default' AS desired_datum, 0 AS was_found
FROM dual
--Put explicit datum on top, if it exists
ORDER BY was_found DESC
) finder
WHERE ROWNUM <=1;
似乎必须有一些惯用的方法来做到这一点,它不依赖于 ORDER BY
的这种奇怪用法,但我找不到它.有谁知道更好的方法吗?
It seems like there must be some idiomatic way to do this which doesn't depend on this strange use of ORDER BY
, but I couldn't find it. Does anyone know of any better methods?
推荐答案
这应该是你所做的更简单的版本:
This should be a simpler version of what you did:
SELECT NVL(desired_datum, 'default') AS desired_datum
FROM DUAL LEFT JOIN data_table ON the_key = &input_value
这篇关于Oracle 11g:当查询不返回任何内容时默认为静态值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Oracle 11g:当查询不返回任何内容时默认为静态值


基础教程推荐
- 带更新的 sqlite CTE 2022-01-01
- while 在触发器内循环以遍历 sql 中表的所有列 2022-01-01
- 如何在 CakePHP 3 中实现 INSERT ON DUPLICATE KEY UPDATE aka upsert? 2021-01-01
- 带有WHERE子句的LAG()函数 2022-01-01
- MySQL 5.7参照时间戳生成日期列 2022-01-01
- ORA-01830:日期格式图片在转换整个输入字符串之前结束/选择日期查询的总和 2021-01-01
- 使用 VBS 和注册表来确定安装了哪个版本和 32 位 2021-01-01
- MySQL根据从其他列分组的值,对两列之间的值进行求和 2022-01-01
- CHECKSUM 和 CHECKSUM_AGG:算法是什么? 2021-01-01
- 从字符串 TSQL 中获取数字 2021-01-01