一种方法是将此查询包装到另一个查询中:
SELECT COUNT(visid) FROM (
SELECT COUNT(visid) AS cvisid, visid
FROM vislog
GROUP BY visid
HAVING cvisid = 2) AS c
但我认为您需要获取访问的直方图:这可以使用 PHP 完成(假设查询与问题中的相同):
$results = array();
// query preparation skipped, as it's obviously done by the OP himself
while ($row = $sth->fetch()) {
$count = $row['cvisid'];
if (isset($results[$count])) {
$results[$count]++;
}
else {
$results[$count] = 1;
}
}
或者使用 MySQL 本身:
SELECT cvisid,
COUNT(cvisid) AS cnt
FROM (
SELECT visid,
COUNT(visid) AS cvisid
FROM vislog
GROUP BY visid ) AS c
GROUP BY cvisid