如果该行不存在,如何从pgsql中的查询返回“ 0”? [英] How can i return '0' from query in pgsql, if the row doesn't exist?

查看:660
本文介绍了如果该行不存在,如何从pgsql中的查询返回“ 0”?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果该行不存在,如何从pgsql中的查询返回 0?
Like


选择是否存在(字段)则字段ELSE 0 FROM table



解决方案

我不完全了解您想要获得什么结果,但是如果您想从表中某行的字段中获得价值;如果没有行,则返回0;请尝试:

  select Coalesce((从表限制1中选择字段),0)

如果表的过滤条件可能返回1行或什么都不返回,请尝试以下查询:

 选择合并((从表中选择<您的条件>的字段,0)


How can i return '0' from query in pgsql, if the row doesn't exist? Like

SELECT IF EXISTS(field) THEN field ELSE 0 FROM table

解决方案

I'm not completely understood what result do you want to get, but if you want to get value from field from some row in table and 0 if there's no rows exists, try:

select coalesce((select field from table limit 1), 0)

if you have some filter condition for table which could return 1 row or nothing, try this query:

select coalesce((select field from table where <your condition>), 0)

这篇关于如果该行不存在,如何从pgsql中的查询返回“ 0”?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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