2014-10-03 32 views
1

有人可以帮我按照“否”的顺序'ORDER'一个mysql表。在同一个表的一列中出现的字符串。mysql:ORDER BY mysql表中某列元素的出现次数

我想根据发生位置的次数重新排列表。

请看下面的例子:

 
id name  location 
-- -----  -------- 
1  Mark  US 
2  Mike  US 
3  Paul  Australia 
4  Pranshu India 
5  Pranav  India 
6  John  Canada 
7  Rishab  India 

预期结果:

 
id name  location 
-- -----  -------- 
4  Pranshu India 
5  Pranav  India 
7  Rishab  India 
1  Mark  US 
2  Mike  US 
3  Paul  Australia 
6  John  Canada 

我尝试此查询:

SELECT *, COUNT(location) AS count FROM `tablename` GROUP BY `location` ORDER BY count DESC; 

但它给我看了以下结果省略重复发生位置的

 
id name  location 
-- -----  -------- 
4  Pranshu India 
1  Mark  US 
3  Paul  Australia 
6  John  Canada 
+1

这是罕见的事情一个数,也是一个事通过组! – Strawberry 2014-10-03 21:47:31

+0

@MindStalker的答案正是你现在需要的东西http://stackoverflow.com/questions/2283305/mysql-order-by-count – Logain 2014-10-03 21:55:43

回答

1
SELECT x.* 
    FROM my_table x 
    JOIN (SELECT location, COUNT(*) total FROM my_table GROUP BY location) y 
    ON y.location = x.location 
ORDER 
    BY total DESC 
    , id; 
相关问题