Malixxl
Malixxl

Reputation: 525

Query order by count

ID Name

i need a query returns group order by count like;

ID Name

Upvotes: 1

Views: 1874

Answers (3)

Boni
Boni

Reputation: 610

Check This

SELECT id, Name FROM user
GROUP BY Name   
ORDER BY COUNT(Name) DESC;

Upvotes: 0

Mahesh Patil
Mahesh Patil

Reputation: 1551

Try this it might be helpful,

SELECT COUNT(Name) AS total, id, Name
FROM table_name
GROUP BY Name   
ORDER BY COUNT(Name) DESC;

Upvotes: 2

John Woo
John Woo

Reputation: 263693

SELECT @rownum:=@rownum+1  AS ID,
       NameList.iName
FROM 
      (SELECT DISTINCT iName, COUNT(iName) AS iCount 
       FROM people 
       GROUP BY iName 
       ORDER BY iCount DESC) NameList, 
      (SELECT @rownum:=0) r 

change this subquery

      (SELECT DISTINCT iName, COUNT(iName) AS iCount 
       FROM people 
       GROUP BY iName 
       ORDER BY iCount DESC)

to the table with the list of name.

Upvotes: 1

Related Questions