MySQL计数和排序行

MySQL计数和排序行,mysql,sorting,count,Mysql,Sorting,Count,对mysql中的一列数据进行计数和排序并将结果输出为html列表的最佳方法是什么 示例数据: **Type** Train Car Car Car Bus Bus 输出应首先按最大计数项排序: - car(3) - bus(2) - train (1) 另外,在大桌子上这样做是不好的做法吗?尝试以下查询: select type, COUNT(*) from data group by type select type, count(*) from vehicles group by

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

示例数据:

**Type**
Train
Car
Car
Car
Bus
Bus
输出应首先按最大计数项排序:

 - car(3)
 - bus(2)
 - train (1)
另外,在大桌子上这样做是不好的做法吗?

尝试以下查询:

select type, COUNT(*) from data group by type
select type, count(*) from vehicles group by type order by count(*) desc
关于:

还有,这是在大桌子上做的坏习惯吗

select type, COUNT(*) from data group by type

这取决于您的桌面引擎。MyISAM使用聚合函数非常快速,如
count(*)
。但是,InnoDB每次都必须扫描表并计数。因此,我不建议
count(*)
ing大型InnoDB表。相反,您可以将“count”变量存储在元表中,并仅在插入/更新时更新它。

尝试以下查询:

select t.type, count(t.type) as typecount from type as t group by type order by typecount desc
希望对这个问题有所帮助