2011-09-26 66 views
0

我有一個包含字段的數據表:search, search_location, num_searches。我想放在一起SELECT語句,將產生100個最流行的search_locations的列表,通過爲所有搜索的num_searchesSUM()確定與同search_location(不管search字段的值)。我怎樣才能做到這一點?使用SQL檢索數據集中最流行的查詢

回答

6

您可以使用GROUP BY,一種通過將共享某些相同值的所有行分組來減少表的方法。

SELECT search_location, SUM(num_searches) as total_searches 
    FROM my_table 
    GROUP BY search_location 
    ORDER BY total_searches DESC 
    LIMIT 100;