我有一个具有以下结构的表:
Table1
+----------------+-------------+
| ID | Condition | Color |
+----------------+-------------+
| 1 | FAIR | RED |
+----------------+-------------+
| 2 | GOOD | Red |
+----------------+-------------+
| 3 | Great | BLACK |
+----------------+-------------+
| .. | .. | .. |
+----------------+-------------+
我想计算所有与颜色有关的条件。所以一个简单的查询将是:
SELECT COUNT(ID) Total FROM Table1 WHERE Condition="FAIR" AND Color="RED"
这将返回条件为FAIR且颜色为RED的行数。是否有可能包括所有可能的结果?那么一个表为一个查询提供所有条件的count(id)而不是多个查询?
+----------------+-------------+
| Total | Condition | Color |
+----------------+-------------+
| 5 | FAIR | RED |
+----------------+-------------+
| 6 | FAIR | BLUE |
+----------------+-------------+
| 2 | Great | RED |
+----------------+-------------+
| .. | .. | .. |
+----------------+-------------+
条件和颜色有一组固定值,空值除外。
答案 0 :(得分:2)
听起来像是在寻找group by
:
SELECT COUNT(ID) Total, Condition, Color
FROM Table1
GROUP BY Condition, Color