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