MySQL 按指定值的出现次数排序 [英] MySQL order by occurrences of a specified value

查看:46
本文介绍了MySQL 按指定值的出现次数排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

SQL Fiddle 示例

首先,我们计算每个产品的总票数,并按 product_id 分组.然后我们得到 issue 列的值以及每个值出现的次数.这一点已经在小提琴中得到了证明.

First, we're counting the total number of tickets per product, and grouping by product_id. Then we're getting the values of the issue column and the number of occurrences of each. This much is already demonstrated in the fiddle.

我需要的是能够按每个问题的出现次数进行排序.例如,我们希望按 broken-partnot-received 数量最多的产品排序,所以它会按 issue 的值排序 列并按每个产品出现该问题的次数排序.

What I need is to be able to order by the number of occurrences of each issue. For example, we want to order by products with the highest number of broken-part or not-received, so it'll order by the value of the issue column and sort by how many times that issue is present for each product.

谢谢!

推荐答案

你可以使用条件求和.

例如

SUM(CASE WHEN `issue` = 'missing-part' THEN `count` ELSE NULL END) missing_part_total

然后按那个排序.

这篇关于MySQL 按指定值的出现次数排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆