I have a table named users
that contains the following columns:
id
login
status
Now I want to create a page of statistics in PHP and I want to see how many users have status=0, how many users have status=1, how many users have status=2.
I want to do this the most efficient possible way, without having to run 3 queries.
Right now I only know to do this with 3 queries in UNION:
(SELECT COUNT(*) FROM users WHERE status='0') UNION (SELECT COUNT(*) FROM users WHERE status='1') UNION (SELECT COUNT(*) FROM users WHERE status='2')
I dont know too much SQL programming but I was thinking that something like this might work:
SELECT IF(status='0',stat0++),IF(status='1',stat1++),IF(status='2',stat2++) FROM users GROUP BY status
But it doesnt work because the syntax is wrong