cusID | Name | status | Date
---------------------------------
1 | AA | 0 | 2013-01-25
2 | BB | 1 | 2013-01-23
3 | CC | 1 | 2013-01-20
SELECT COUNT(cusID) FROM customer WHERE STATUS=0;
SELECT COUNT(cusID) FROM customer WHERE STATUS=1;
有没有办法梳理这两个sql并将结果作为一个返回。因为每次都要避免调用DB。我尝试了两个法规的UNION,但只显示了一个结果。
答案 0 :(得分:3)
这是MySQL中最短的解决方案。
SELECT SUM(status = 1) totalActive,
SUM(status = 0) totalInactive
FROM tableName
这是CASE
版本
SELECT SUM(CASE WHEN status = 1 THEN 1 ELSE 0 END) totalActive,
SUM(CASE WHEN status = 0 THEN 1 ELSE 0 END) totalInactive
FROM tableName