计算出哪个字符串最多

时间:2014-01-23 02:39:15

标签: mysql sql database

我有一张桌子例如:

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

我想根据它们出现的次数显示名称

3 个答案:

答案 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`