I have a quick question. I know that we cannot use WHERE
clause in an aggregate function in MySQL. The table structure is as follows:
+----+------+----------+--------+
| ID | Name | Location | Active |
+----+------+----------+--------+
| 1 | Aaaa | India | 0 |
| 2 | Aaaa | USA | 0 |
| 3 | Aaaa | USA | 1 |
| 4 | Aaaa | India | 0 |
| 5 | Aaaa | UK | 0 |
| 6 | Aaaa | India | 1 |
| 7 | Aaaa | USA | 1 |
| 8 | Aaaa | USA | 0 |
| 9 | Aaaa | India | 0 |
| 10 | Aaaa | UK | 1 |
+----+------+----------+--------+
The query I have here is:
SELECT COUNT(*), `location`, `active` FROM `users` GROUP BY `location`;
The above query will give me the counts of the location. But, I need only the active users. So, I need a WHERE
clause that does something like:
SELECT COUNT(*), `location`, `active` FROM `users` GROUP BY `location` WHERE `active`=1;
The above query is invalid. The valid query would be using HAVING
. But, if I change the query to:
SELECT COUNT(*), `location`, `active` FROM `users` GROUP BY `location` HAVING `active`=1;
The counts are no different from the original query, which is:
SELECT COUNT(*), `location`, `active` FROM `users` GROUP BY `location`;
So, what am I supposed to do for getting the user counts of the location, who are active? Thanks in advance.