oracle 在查询中找不到匹配项时如何显示默认值?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8200462/
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
How to display a default value when no match found in a query?
提问by Hariharbalaji
I want to display a default message when there is no data obtained from a query.
当没有从查询中获得数据时,我想显示一条默认消息。
For example Let us take a query
例如让我们做一个查询
select empname from employee where id = 100
从员工中选择员工姓名,其中 id = 100
If no data matches this search i want to get Unavailable
as a result or the required result should display.
如果没有数据与此搜索匹配,我想获得Unavailable
结果或应显示所需的结果。
So how should i write a SQL query to achieve this.
那么我应该如何编写 SQL 查询来实现这一点。
I am using Oracle 10g.
我正在使用 Oracle 10g。
回答by Erwin Brandstetter
SELECT COALESCE((SELECT empname FROM employee WHERE id = 100), 'Unavailable')
FROM DUAL;
You have to wrap the SELECT
into another SELECT
or no row will be returned. And where there is no row, there cannot be a value.
您必须将 包装SELECT
成另一个,SELECT
否则不会返回任何行。在没有行的地方,就不会有值。