問題描述
我有一個表格,通過向每行的國家/地區代碼"列添加國家/地區代碼來記錄我的 Web 應用程序中的用戶位置.每行代表對特定區域的訪問.
I have a table that is recording user locations from my web application by adding a country code to the 'countrycode' column of each row. Each row is representing a visit to a particular area.
所以我有一些數據,比如
So I have some data like
COL1 COL2 COL3 countrycode
asd asd asd NZ
asd asd asd NZ
asd asd asd NZ
asd asd asd US
asd asd asd US
我想要做的是查詢這個表并在下面顯示類似這樣的內容
What I want to do is to query this table and show me something like this below
Country Count
NZ 3
US 2
但我需要它能夠為出現的更多國家/地區代碼添加一行.我無法理解如何做到這一點,我知道我需要以某種方式使用 COUNT() 函數......
But I need it to be able to add a row for any more country codes that have come up. I cant get my head around a way to do this, I know I need to use the COUNT() function somehow...
推薦答案
要獲得示例輸出,您可以使用 GROUP BY
和 COUNT()
To get your example output, you can use GROUP BY
and COUNT()
SELECT Country, COUNT(*)
FROM myTable
GROUP BY Country
COUNT(*)
將計算每一行,GROUP BY Country
將按國家/地區拆分結果.結果將是基于表中數據的動態結果,因此如果您在表中添加具有不同國家/地區的更多記錄,則無需更改查詢.
COUNT(*)
will count ever row and GROUP BY Country
will split the results out by Country. The results will be dynamic based on the data in the table so you don't need to change your query if you add more records in the table with different countries.
請參閱在線圖書分組依據
這篇關于計數和組合行的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!