查询 type 对应 blog 数量并排序
为了统计每个 type 对应的 blog 数量并按数量降序排序,我们可以使用 sql 查询的聚合函数。
解决方案
使用以下 sql 查询:
select type_id, count(*) c from blog group by type_id order by c
解释
- 该查询从 blog 表中选择 type_id 和 count(*),其中 count(*) 聚合了每个 type_id 对应的 blog 数量。
- group by type_id 将结果按 type_id 分组,这样每个 type_id 都对应一个总计数。
- order by c 对结果按聚合计数 c 降序排序,从而按 blog 数量对 type 进行排序。
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END