计算不同的值

时间:2017-12-27 03:06:04

标签: mysql sql

resulttable:

+-------------+-----------+
| resultSetID | projectID |
+-------------+-----------+
|           1 |         1 |
|           1 |         2 |
|           1 |         3 |
|           2 |         1 |
|           2 |         2 |
|           3 |         1 |
|           3 |         2 |
|           3 |         3 |
+-------------+-----------+

查询:

SELECT
    COUNT( projectID ) 
FROM
    resulttable
WHERE
    projectID = 3

...正确返回2.但是,我想要不使用WHERE条件的每个ID的计数,我该怎么做?

3 个答案:

答案 0 :(得分:1)

你只想要group by吗?

SELECT projectId, COUNT( * ) 
FROM resulttable
GROUP BY projectID;

答案 1 :(得分:0)

以下查询用于获取projectId的所有数据,但如果您想获取不包含where的特定查询数据,则可以使用instead

SELECT projectId, COUNT( * ) 
FROM resulttable
GROUP BY projectId having projectId = 3;

答案 2 :(得分:-1)

以下查询适合您:

SELECT projectID, SUM(resultSetID) 
FROM resulttable
GROUP BY projectID;