how to group by and count using MySQL

后端 未结 4 1062
死守一世寂寞
死守一世寂寞 2021-01-29 01:51

I have data which looks like this:

ID  post_author post_title  guid
3309    21  Should somebody not yet on SQL 2008 wait for SQL 2008 R2, since it\'s near release?  h         


        
相关标签:
4条回答
  • 2021-01-29 01:52
    SELECT COUNT(POST_AUTHOR) AS AUTHOR_COUNT, GUID FROM TABLE_NAME GROUP BY GUID
    
    0 讨论(0)
  • 2021-01-29 02:05

    The problem is how to extract the root part of the URL. If we can be sure that every URL will have at least 3 slashes, this will work, using substring_index

    select substring_index(guid,'/',3) as site, count(id) as authors from table
    group by substring_index(guid,'/',3) 
    

    Of course, if you add an extra column with the site only at insert time, everything will be faster, cleaner and safer (you won't have to complexify the query to handle guids with only two slashes)

    0 讨论(0)
  • 2021-01-29 02:07

    It may be possible to construct such a query but will be not optimized.

    You should add a column to your table which will have an ID of the site. Then add a new table which will have a preparsed data for the site: domain, path, resource, whether http or https, etc

    This way you can be more flexible in searches and will be much faster, since I assume you have few inserts and large number of reads.

    0 讨论(0)
  • 2021-01-29 02:13

    Write a SQL FUNCTION - call it for instance, guid_extract(guid), which extracts the pertinent info, then you can add it to a column in your select::

    SELECT stuff, otherstuff, guid_extract(guid) as site
      ...
      GROUP BY site;
    
    0 讨论(0)
提交回复
热议问题