2010-05-10 67 views
1

对mysql中的数据列进行计数和排序并将结果输出为html列表的最佳方式是什么?MySQL计数和排序行

示例数据:

**Type** 
Train 
Car 
Car 
Car 
Bus 
Bus 

输出应与计数最大的项目首先进行排序:

- car(3) 
- bus(2) 
- train (1) 

而且,这是不好的做法做就大表?

回答

7

尝试此查询:

select type, count(*) from vehicles group by type order by count(*) desc 

在问候:

而且,这是不好的做法做就大表?

这取决于你的表引擎。 MyISAM速度非常快,集成功能如count(*)。但是,InnoDB必须每次扫描表并计数。所以我不建议count(*)一个大的InnoDB表。相反,您可以在元表中存储“count”变量并仅在插入/更新时进行更新。

2
select type, COUNT(*) from data group by type 
+0

短而甜! – Aditya 2010-05-10 16:24:37

0

尝试此查询:

select t.type, count(t.type) as typecount from type as t group by type order by typecount desc 

希望帮助这个查询...!