mysql - Mysql Limit By sum of count
问题描述
我在 mysql 表 id、状态和计数中有 3 列字段我想获得状态组和具有不同 id 值的计数总和示例如下
id - status - count
1 - test1 - 1
1 - test1 - 1
1 - test2 - 1
1 - zer1 - 0
2 - exam1 - 1
2 - exam1 - 1
2 - zer2 - 0
2 - exam2 - 1
3 - mit1 - 1
3 - mit2 - 1
3 - zer3 - 0
4 - term1 - 1
我想要如下结果
id - status - count
1 - test1 - 2
2 - exam1 - 2
3 - mit1 - 1
4 - term1 - 1
我尝试了以下查询的结果,但我没有得到结果
SELECT id,status,sum(count) as count FROM `test` where count=1 group by status order by id
对于上述查询,我得到以下结果
id - status - count
1 test1 2
2 exam1 2
2 exam2 1
3 mit1 1
3 mit2 1
4 term1 1
我无法找到正确的查询来删除重复的 ID 并按计数总和排序。请指导我得到结果。
解决方案
您可以在此处使用多个查询,例如:
Select distinct id from
(SELECT id,status,
sum(count) as count
FROM `test` where count=1 group by status order by id
)
推荐阅读
- javascript - 创建新数组,其中带有逗号的项目被分隔为数组中的唯一项目
- django-forms - django checkboxSelectMultiple
- javascript - 如何在 Jquery 中切换设置的间隔函数?
- javascript - javascript 不会在 slick.js 中呈现箭头按钮
- php - PHP7,将 mySQL 导出为 CSV 不正确显示特殊字符
- javascript - 将站点部署到 Heroku 后,无法连接到外部 API,但可以在本地工作(Node.js)
- python - OAuth 的烧瓶舞
- php - AJAX 新手:按钮单击 PHP
- asp.net-mvc - 锚标记助手链接到 API 控制器
- c# - 如何在实体框架 linq“.Include”中包含父级的 FirstOrDefault 子级