MySQL使用group by选择查询中的总行数

时间:2017-10-07 16:41:17

标签: mysql

我需要知道查询的总结果,我正在使用COUNT() 问题是在这个查询中我有一个GROUP BY

而是获得5作为结果我得到5行,每行的结果总数

SELECT  COUNT(c.id) AS tot
FROM    contributors AS c LEFT JOIN contributors_info AS ci 
        ON c.id = ci.idcontributor
WHERE   c.state = 'active'
GROUP BY c.id
ORDER BY c.id ASC

我的查询结果是

|c.id|
| 1  |
| 2  |
| 1  |
| 1  |
| 1  |

1 个答案:

答案 0 :(得分:1)

尝试以下查询。

 SELECT COUNT(*) as tot FROM(
    SELECT  c.id 
    FROM    CONTRIBUTORS AS c LEFT JOIN contributors_info AS ci 
        ON c.id = ci.idcontributor
    WHERE   c.state = 'active'
    GROUP BY c.id
    ORDER BY c.id ASC
) AS tmp