mysql - MySQL 按计数排序而不对结果进行分组
问题描述
我有以下 MySQL 表:
id| title
1 | Apple
2 | Apple
3 | Cherry
4 | Banana
5 | Banana
6 | Apple
我想按相同字段“标题”的数量对它们进行排序。
所以结果应该是这样的:
1 | Apple
2 | Apple
6 | Apple
4 | Banana
5 | Banana
3 | Cherry
感谢您的帮助
解决方案
请参阅下面的查询:
select
mt.id,
mt.title
from mytable mt
join (
select
title,
count(*) as ct
from mytable
group by title
) sq on mt.title = sq.title
order by sq.ct DESC, id ASC
推荐阅读
- java - 用junit运行柏树
- omnet++ - 构建 INET、Veins、Veins_inet3、SimuLTE 以集成到我的模拟时出错
- python - 使用python将文件上传到s3存储桶时权限被拒绝
- python - 如何使用 ax.get_subplotspec().is_first_col() 而不是 is_first_col
- java - 如何更改 javafx 警告框的图标
- installation - ROS Noetic 环境设置(目录未找到)
- c# - 在转换为枚举的对象上调用重载函数
- c++ - 如何获取键盘名称?(在视觉上区分特定语言的键盘)
- pandas - 我怎样才能像这样组合列和行
- css - 谁能帮我解决这个设计问题?