Jump to content

Order data by # of results


Suchy

Recommended Posts

id |  who  | when

--+-------+------

1  | mike  | wed

2  | mike  | sat

3  | rob    | sat

4  | bob    | mon

5  | steve  | sun

6  | anna  | thu

7  | anna  | wed

8  | carl    | wed

9  | anna  | fri

10| rob    | tue

 

I would like to dislay all this data and arrange it from the person who loged-in the most to the person who loged-in the least.

The results should be:

anna, mike, rob, steve, bob, carl

 

How should I do this?

SELECT * FROM logins ORDER BY ??????

 

Link to comment
https://forums.phpfreaks.com/topic/108299-order-data-by-of-results/
Share on other sites

You need to form groups using the who column, count how many rows are in each group, then order by the resulting counts -

 

SELECT who, COUNT(*) AS cnt FROM logins GROUP BY who ORDER BY cnt DESC

 

If you don't want the resulting counts to appear in the result set, a slightly simpler version -

 

SELECT who FROM logins GROUP BY who ORDER BY COUNT(*) DESC

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.