user2666633
user2666633

Reputation: 340

Counts which string appears the most

I have a table for example:

Names  Details
--------------
wilson admin
david  member
wilson admin
wilson admin
sam    member
david  member

Now what i want to achieve is show a table like this

Names  Details count
--------------------
wilson admin   3
david  member  2    
sam    member  1

I want to show the names according to how many times they appear

Upvotes: 0

Views: 39

Answers (3)

StuartLC
StuartLC

Reputation: 107357

SELECT Name, Detail, COUNT(*) as Count
FROM MyTable
GROUP BY Name, Detail
ORDER BY Count DESC;

Since you have more than one column in your output table SELECT, you should group by both columns.

Upvotes: 1

Tan Hong Tat
Tan Hong Tat

Reputation: 6879

Try this, use the GROUP BY and COUNT

SELECT Names, Details, COUNT(NAMES) as `count` FROM `table` GROUP BY `Names`

Upvotes: 0

user2666633
user2666633

Reputation: 340

I found the answer already

  SELECT names, COUNT(names) AS count
  FROM my_table
  GROUP BY names ORDER BY count DESC

Upvotes: 0

Related Questions