MYSQL count of count?

后端 未结 5 2124
执笔经年
执笔经年 2020-12-10 19:36

I have a mysql table like:

id, visitorid, pageid

When a visitor hits the website it stores their visitor id and the page id as a row.

5条回答
  •  没有蜡笔的小新
    2020-12-10 20:27

    You can wrap your query inside another one:

    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 ;
    

    or (I suppose this makes more sense for passing the numbers to a chart), remove the ORDER BY which is the same as putting:

    ORDER BY cnt ;
    

提交回复
热议问题