I’m trying to run a report on our database. We want to know the new registrations per industry per month. I’ve written this query:
SELECT
COUNT(j.jobseeker_id) as new_registrations,
i.description as industry_name,
MONTHNAME(j.created_at)
FROM
tb_jobseeker as j, tb_industry as i
WHERE
YEAR(j.created_at) = 2009
AND
i.industry_id = j.industry_id
GROUP BY
i.description, MONTHNAME(j.created_at)
HAVING
MONTHNAME(j.created_at) = MONTHNAME(NOW());
When I run this query, I get an empty result set. However, if I run the following:
SELECT
COUNT(j.seeker_id) as new_registrations,
i.description as industry_name,
MONTHNAME(j.created_at)
FROM
tb_seeker as j, tb_industry as i
WHERE
YEAR(j.created_at) = 2009
AND
i.industry_id = j.industry_id
GROUP BY
i.description, MONTHNAME(j.created_at)
HAVING
MONTHNAME(j.created_at) = 'June';
It returns the results I’m looking for.
Any help please? I’m stumped.
Update: the query will be run at the end of every month or start of the next for the past month. So, we’re now in June, but it needs to run for May. Hope that makes sense.
Errr…. it’s not May O_o
Your query will be much more efficient if you write it like this:
You should only use HAVING if you absolutely HAVE to.
It would be even better if you could group by i.id instead of i.description, but that depends on whether i.description is unique.