How to combine three different queries in mysql when there are several criteria?

I have a table and I need to get the number of rows using different criteria. I am currently using 3 queries one by one:

  • SELECT COUNT (status) FROM projects WHERE project = '1'
  • SELECT COUNT (status) FROM projects WHERE project = '1' AND status> '10 '
  • SELECT COUNT (status) FROM projects WHERE project = '1' AND status> '20 '

How to combine these queries into one query?

PS There are 30 different statuses, so GROUP BY status is not a big choice.

+4
source share
1 answer

UNION :

SELECT COUNT(status), ('1') as info FROM projects WHERE project='1'
UNION
SELECT COUNT(status), ('1-10') as info FROM projects WHERE project='1' AND status>'10'
UNION
SELECT COUNT(status), ('1-20') as info FROM projects WHERE project='1' AND status>'20'

:

https://dev.mysql.com/doc/refman/4.1/en/union.html

+5

All Articles