SQL, subquery if null

Asked

Viewed 452 times

2

I’m doing a query, but I didn’t want it to return NULL.

What I got is this:

select ifnull(sum(job_withdraw_qty),0) as total,

 (
   select IFNULL(sum(job_withdraw_qty),0) 
   from job_positions 
   where DATE_FORMAT(job_positions.created_on, '%Y-%m-%d') > '2016-01-01'
   and is_hired = 1
   and jobs.job_origem = 730

 ) as aumento

 from job_positions
 inner join jobs
 on job_positions.job_id = jobs.job_id
 where DATE_FORMAT(job_positions.created_on, '%Y-%m-%d') > '2016-01-01'
 and is_hired = 1

Only when executing the query:

inserir a descrição da imagem aqui

In the total field all is well but in the increase no, someone knows a solution?

  • the ifnull has to stay out of parenthesis: ifnull(select ...), 0) as aumento

1 answer

1


The ifnull has to stay out of the parenthesis of subquery:

ifnull((select ...), 0) as aumento

Browser other questions tagged

You are not signed in. Login or sign up in order to post.