2015-09-25 35 views

回答

3

使用Group by計數的類別。

然後命令該結果以降序計數的順序設置,並選擇頂部1.

查詢

select t.category 
from 
(
    select category, 
    count(category) as cat_count 
    from fruits 
    group by category 
)t 
order by t.cat_count desc limit 1; 

SQL Fiddle

如果具有多個類別相同的最高計數。然後,

查詢

select t.category 
from 
(
    select category, 
    count(category) as cat_count 
    from fruits 
    group by category 
)t 
where t.cat_count = 
(
    select count(category) as cat_count 
    from fruits 
    group by category 
    order by count(category) desc 
    limit 1 
); 

SQL Fiddle

+0

非常感謝! – conan

0

加限制,如果你需要頂級1,2,3這樣的極限X1 x是1,2,3

Select count(*) as total,category from table group by category from table order by count(*) desc 
+0

OP需要具有最大數量的類別名稱。 – Wanderer