在查询中找不到匹配项时如何显示默认值? [英] How to display a default value when no match found in a query?

查看:120
本文介绍了在查询中找不到匹配项时如何显示默认值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当没有从查询获得的数据时,我想显示默认消息.

I want to display a default message when there is no data obtained from a query.

例如 让我们进行查询

从id = 100的员工中选择empname

select empname from employee where id = 100

如果没有数据与该搜索匹配,我希望得到Unavailable作为结果,或者应该显示所需的结果.

If no data matches this search i want to get Unavailable as a result or the required result should display.

所以我应该如何编写SQL查询来实现这一目标.

So how should i write a SQL query to achieve this.

我正在使用Oracle 10g.

I am using Oracle 10g.

推荐答案

SELECT COALESCE((SELECT empname FROM employee WHERE id = 100), 'Unavailable')
FROM   DUAL;

您必须将SELECT包装到另一个SELECT中,否则将不返回任何行.没有行的地方就没有值.

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.

这篇关于在查询中找不到匹配项时如何显示默认值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆