[英]displaying a value even if no rows returned
We have a situation in oracle SQL.我们在oracle SQL 中有一个情况。
select x from A where x=10;
so if 10
exists in the column, value will be displayed 10
.所以如果列中存在
10
,值将显示10
。 but if it doesn't exists I want to display null
.但如果它不存在,我想显示
null
。 I tried NVL
but it is not returning anything as data is not displaying.我尝试过
NVL
但由于未显示数据,因此它没有返回任何内容。
Could you please help me to resolve this.你能帮我解决这个问题吗?
If you want to return multiple rows from the original SELECT
then you can use UNION ALL
to add a row when the value does not exist:如果要从原始
SELECT
返回多行,则可以在值不存在时使用UNION ALL
添加一行:
SELECT x
FROM A
WHERE x = 10
UNION ALL
SELECT NULL
FROM DUAL
WHERE NOT EXISTS (
SELECT x
FROM A
WHERE x = 10
)
If your table is:如果您的表是:
CREATE TABLE a ( x ) AS
SELECT 10 FROM DUAL UNION ALL
SELECT 10 FROM DUAL UNION ALL
SELECT 10 FROM DUAL;
Then the query will output:然后查询将输出:
\n|| X |
X |\n|
| -: |
-: |\n|
| 10 |
10 |\n|
| 10 |
10 |\n|
| 10 |
10 |\n
Then if you DELETE FROM a;
那么如果你
DELETE FROM a;
and repeat the query, it will output:并重复查询,它将输出:
\n|| X |
X |\n|
| ---: |
---: |\n|
| null |
空|\n
Try this:尝试这个:
select max(x)
from A
where x = 10; --this will return null if no data
And then you can do a NVL:然后你可以做一个NVL:
select nvl(max(x), 0)
from A
where x = 10; --this will return 0 if no data
Where
clause filter out the data what you are looking for, if it doesn't exists then query will return no record. Where
子句过滤掉您要查找的数据,如果它不存在,则查询将不返回任何记录。
So, you can remove where
clause & do aggregation :因此,您可以删除
where
子句并进行聚合:
select max(case when a.x = 10 then 10 end)
from a;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.