Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/xamarin/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
MySQL-如果select子查询中的计数为null,则更改为0_Mysql_Sql_Count_Subquery_Aggregate Functions - Fatal编程技术网

MySQL-如果select子查询中的计数为null,则更改为0

MySQL-如果select子查询中的计数为null,则更改为0,mysql,sql,count,subquery,aggregate-functions,Mysql,Sql,Count,Subquery,Aggregate Functions,我有以下疑问: select `jobs`.*, (SELECT COUNT(user_jobs_application.id) as count FROM user_jobs_application join job_shifts on job_shifts.id = user_jobs_application.job_shift_id where job_shifts.jobs_id = jobs.id and user_jobs_application.status = 1 group

我有以下疑问:

select `jobs`.*, 
(SELECT COUNT(user_jobs_application.id) as count FROM user_jobs_application
join job_shifts on job_shifts.id = user_jobs_application.job_shift_id
where job_shifts.jobs_id = jobs.id
and user_jobs_application.status = 1
group by user_jobs_application.users_id
) as entries
from `jobs` where `jobs`.`deleted_at` is null order by `id` desc limit 25 offset 0

select中的子查询将给出null,而不是0。我是否可以更改此值,以便如果值为null,它将显示0?

从子查询中删除
group by
子句就足够了。无论如何都不需要它,因为它在您筛选的列上分组(而且它是必需的,那么我的意思是子查询可能返回多行,这将生成运行时错误)

对查询的其他更改:

  • 大概,
    user\u jobs\u应用程序(id)
    不可为空;如果是这样,
    count(*)
    就足够了,而且比
    count(user\u jobs\u application.id)

  • 表别名使查询更易于读取和写入


感谢您的帮助和解释:)
select 
    j.*, 
    (
        select count(*) as count 
        from user_jobs_application uja
        join job_shifts js on js.id = uja.job_shift_id
        where js.jobs_id = j.id and uja.status = 1
    ) as entries
from jobs j
where j.deleted_at is null 
order by id desc limit 25 offset 0