sql – 从没有行的计数获取0值

我有SELECT:
SELECT c FROM (
    SELECT "candidate_id" as id,count("candidate_id") as c
    FROM "Applicaions"
    GROUP BY "candidate_id"
) as s WHERE id= _SOME_ID_;

但是,如果count>如果count = 0,则不返回任何内容.如何获得0的“候选人”没有任何应用程序?

有表“候选人”.
如果候选人没有应用程序或不存在,我需要得到0.

编辑

我现在有

SELECT COALESCE ((SELECT count("candidate_id") as c
FROM "Applicaions" WHERE "candidate_id"=_SOME_ID_
GROUP BY "candidate_id"),0);

它工作完美.但是可以写得更简单还是最好的解决方案?我应该创建任何索引吗?

解决方法

您需要在Postgresql http://developer.postgresql.org/pgdocs/postgres/functions-conditional.html中使用COALESCE功能

基本上你需要告诉sql如何处理NULL.即当NULL返回0时.

相关文章

SELECT a.*,b.dp_name,c.pa_name,fm_name=(CASE WHEN a.fm_n...
if not exists(select name from syscolumns where name=&am...
select a.*,pano=a.pa_no,b.pa_name,f.dp_name,e.fw_state_n...
要在 SQL Server 2019 中设置定时自动重启,可以使用 Window...
您收到的错误消息表明数据库 'EastRiver' 的...
首先我需要查询出需要使用SQL Server Profiler跟踪的数据库标...