I would like to count different conditions in one table:
SELECT gt.smechanic as 'Mech ID',
gm.mechanic_name as 'Mech Name',
count(gt.smechanic) as 'Total Invoices Not Received',
count(if(datediff(gt.dispatch_date, curdate()) < 10,1,0)) as 'Within 10 days'
from gt
straight_join gm on gt.smechanic = gm.mechanic_id
where gt.status = 3 or gt.status = 5
group by gt.scheduled_mechanic
I am not sure if I am cancelling myself out with this statement because in both columns that I am counting the same value is returned.
You need to use
SUM()in the second column, notCOUNT(): COUNT merely counts records, it doesn’t care if the value in them is 0 or 1. SUM, however, will return the total of all records that match your criterion.Let me also point out that selecting non-aggregate columns that you aren’t also grouping by leaves you vulnerable to unpredictable results. I would change that GROUP BY clause to