如果我有一个简单的数据库表,如下所示:
--------------
| NAME |
--------------
| Andrew |
| Bill |
| Andrew |
| Claire |
| Claire |
| Andrew |
--------------
是否可以运行会产生名称计数的查询?即。
-----------------------
| NAME | COUNT |
-----------------------
| Andrew | 3 |
| Claire | 2 |
| Bill | 1 |
-----------------------
答案 0 :(得分:9)
像这样:
SELECT Name, COUNT(Name) FROM TABLE GROUP BY Name
答案 1 :(得分:3)
您可能希望这获得最大值:
SELECT Name, COUNT(Name) AS c
FROM TABLE
GROUP BY Name
ORDER BY c DESC