My Query
$sql = 'SELECT status FROM tablename';
And the result
------------
status
------------
assigned
assigned
assigned
assigned
assigned
accepted
accepted
completed
completed
completed
completed
completed
Now i can find the total count of each status
SELECT status, COUNT(status) AS cnt
FROM tname
GROUP BY b.statusName
HAVING (cnt >= 1)
This will give
status cnt
--------------
accepted 2
assigned 5
completed 5
How do i sum only completed
and accepted
count?
This is called conditional summing, when you place the condition within the sum() function:
SELECT SUM(IF(status IN ('accepted','assigned'),cnt,0)) as sum_of_acc_asg
FROM
(SELECT status, COUNT(status) AS cnt
FROM tname
GROUP BY b.statusName
HAVING (cnt > 1)) t
Or you can use where to filter the subquery first:
SELECT SUM(cnt) as sum_of_acc_asg
FROM
(SELECT status, COUNT(status) AS cnt
FROM tname
WHERE status IN ('accepted','assigned')
GROUP BY b.statusName
HAVING (cnt > 1)) t
Its simple, replace group by
by where
SELECT status, COUNT(status) AS cnt
FROM tname
WHERE b.statusName IN ('completed','accepted')
HAVING (cnt > 1)
从tname中选择COUNT(状态)AS cnt status =“已完成”或status =“已接受”
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.