SQL- show null, 0 and not present value

That’s a left join:

select u.name, count(t.owner) cnt
from app_users u
left join app_tickets t
    on  t.owner = u.id
    and t.status in (0, 1, 2, 4, 5)
group by u.name, u.id

You could also use a correlated subqueryu:

select u.name,
    (select count(*) from app_tickets t where t.owner = u.id and t.status in (0,1,2,4,5)) cnt
from app_users u

This query would take advantage of an index on app_tickets(owner, status).

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top