我有一个像以下的mysql表:
id, visitorid, pageid
当访问者访问网站时,它会将访问者ID和页面ID存储为一行。
我正在尝试将访问网站的访问者数量提取X次。 (图表)。所以有多少人只访问一页,有多少人访问2页......
到目前为止,我有:SELECT COUNT(visid),visid FROM vislog GROUP BY visid ORDER BY COUNT(visid) DESC
但我不知道如何计算计数的下一部分。
可以作为mysql查询吗?
编辑: 我有added my answer。
答案 0 :(得分:13)
您可以将查询包装在另一个中:
SELECT
cnt AS page_visits
, COUNT(*) AS number_of_visitors
FROM
( SELECT
COUNT(*) AS cnt --- use: COUNT(DISTINCT page_id)
--- for a different count
FROM vislog
GROUP BY visid
) AS grp
GROUP BY cnt
ORDER BY number_of_visitors ;
或(我认为这对于将数字传递给图表更有意义),删除与放置相同的ORDER BY
:
ORDER BY cnt ;
答案 1 :(得分:9)
一种方法是将此查询包装到另一个中:
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
答案 2 :(得分:0)
尝试使用以下查询
SELECT COUNT(a.page_visits) AS no_of_visitors, a.page_visits AS page_count
FROM(
SELECT COUNT(DISTINCT pageid) AS page_visits
FROM vislog
GROUP BY visid) AS a
GROUP BY a.page_visits;
希望它有所帮助...
答案 3 :(得分:0)
Raina77ow(第二个回复)在他的第三个代码块中返回了一个干净的解决方案
SELECT cvisid,
COUNT(cvisid) AS cnt
FROM (
SELECT visid,
COUNT(visid) AS cvisid
FROM vislog
GROUP BY visid ) AS c
GROUP BY cvisid
谢谢
答案 4 :(得分:-6)
我可以这样解决:
SELECT cnt, COUNT(cnt) FROM (
SELECT COUNT(visid) as cnt FROM vislog GROUP BY visid
) x GROUP BY cnt ORDER BY cnt ASC
小x
很重要。