[英]how to group by and count using MySQL
我有看起來像這樣的數據:
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? http://sql.stackexchange.com/questions/379/should-somebody-not-yet-on-sql-2008-wait-for-sql-2008-r2-since-its-near-release 1695 429 How do we politely decline well meaning advice from the Grandmother? http://moms4mom.stackexchange.com/questions/1208/how-do-we-politely-decline-well-meaning-advice-from-the-grandmother 556 173 Books on how to be a great dad http://moms4mom.stackexchange.com/questions/1042/books-on-how-to-be-a-great-dad 160 30 Building an ice hockey net cam http://photo.stackexchange.com/questions/8/building-an-ice-hockey-net-cam 159 30 Generic commercial photo release form http://photo.stackexchange.com/questions/4/generic-commercial-photo-release-form
我需要創建一個查詢,該查詢將GUID字段(根URL)的一部分數據分組,並為每個計數POST_AUTHOR。
我正在尋找的結果將是這樣的:
Site Count of Authors http://sql.stackexchange.com 1 http://moms4mom.stackexchange.com 2 http://photo.stackexchange.com 2
如果有人幫助我構造sql,我將不勝感激。
SELECT COUNT(POST_AUTHOR) AS AUTHOR_COUNT, GUID FROM TABLE_NAME GROUP BY GUID
可能會構造這樣的查詢,但不會進行優化。
您應該在表中添加一列,該列將具有站點的ID。 然后添加一個新表,該表將具有站點的預備數據:域,路徑,資源,http還是https等
這樣,您就可以更加靈活地進行搜索,並且速度會更快,因為我假設您插入的次數很少,讀取次數很多。
編寫一個SQL函數-調用它,例如guid_extract(guid),它提取相關信息,然后可以將其添加到選擇的列中:
SELECT stuff, otherstuff, guid_extract(guid) as site
...
GROUP BY site;
問題是如何提取URL的根部分。 如果我們可以確保每個URL至少包含3個斜杠,則可以使用substring_index來工作
select substring_index(guid,'/',3) as site, count(id) as authors from table
group by substring_index(guid,'/',3)
當然,如果僅在插入時在網站上添加額外的列,則一切將變得更快,更干凈和更安全(您不必復雜化查詢以僅使用兩個斜杠來處理guid)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.