使用distinct和count的MYSQL Select语句

时间:2015-05-13 09:36:06

标签: mysql

您好我是mysql的初学者。我有这样一张桌子

|Item no.|Location|
-------------------
|192     |Japan   |
|882     |Korea   |
|982     |China   |
|111     |China   |
|02      |Japan   |
|03      |Korea   |
|04      |Japan   |

我希望结果表格像这样

|Location|NumberofItems|
------------------------
|Japan   |      3      |
|Korea   |      2      |
|China   |      2      |

我的代码似乎无法运作。这是我的代码:

SELECT distinct Location, count(*) as NumberofItems FROM Products inner join Products on Price = Price

2 个答案:

答案 0 :(得分:1)

如果按特定列分组,则只能获得该列中的不同值。然后,您可以在每个组

上使用count等聚合函数
SELECT Location, count(*) as NumberofItems 
FROM Products
GROUP BY Location

答案 1 :(得分:0)

您需要GROUP BY命令。

SELECT distinct location,count(*) as NumberofItems FROM products GROUP BY location;