Reputation: 365
In my table I have task
, every task
have state
.
1 - planned
2 - executing
3 - finished
I want get count of all planned, executing and finished task
.
I can write three queries like this:
SELECT COUNT(*) FROM `task` WHERE state = 1
SELECT COUNT(*) FROM `task` WHERE state = 2
SELECT COUNT(*) FROM `task` WHERE state = 3
So, my question is: It is possible (and how?) get this data in one query?
Thank you for any help.
Upvotes: 0
Views: 64
Reputation: 24146
another approach is to use group by, like this:
select state, count(*)
from task
group by state
Upvotes: 6
Reputation: 4824
you use SUM
select state,
sum(case when state = 1 then 1 else 0 end) state1count,
sum(case when state = 2 then 1 else 0 end) state2count,
sum(case when state = 3 then 1 else 0 end) state3count
from task
Upvotes: 5