我有一张桌子例如:
Names Details
--------------
wilson admin
david member
wilson admin
wilson admin
sam member
david member
现在我要实现的是显示一个像这样的表
Names Details count
--------------------
wilson admin 3
david member 2
sam member 1
我想根据它们出现的次数显示名称
答案 0 :(得分:1)
SELECT Name, Detail, COUNT(*) as Count
FROM MyTable
GROUP BY Name, Detail
ORDER BY Count DESC;
由于输出表SELECT
中有多个列,因此应按两列进行分组。
答案 1 :(得分:0)
我已经找到了答案
SELECT names, COUNT(names) AS count
FROM my_table
GROUP BY names ORDER BY count DESC
答案 2 :(得分:0)
试试这个,使用GROUP BY和COUNT
SELECT Names, Details, COUNT(NAMES) as `count` FROM `table` GROUP BY `Names`