分组依据 - 覆盖结果中每个组下决定行的默认行为

发布于 2024-10-04 13:29:03 字数 1774 浏览 2 评论 0原文

进一步扩展这个问题查询以查找每个类别中评分最高的文章 -

考虑同一个表 -

id | category_id | rating
---+-------------+-------
 1 |           1 |     10
 2 |           1 |      8
 3 |           2 |      7
 4 |           3 |      5
 5 |           3 |      2
 6 |           3 |      6

有一个表articles,其中包含字段id、 rating(1-10的整数)和category_id(代表其所属类别的整数)。如果我有相同的目标,即在每个查询中获得评分最高的文章(这应该是结果):-

期望结果

id | category_id | rating
---+-------------+-------
 1 |           1 |     10
 3 |           2 |      7
 6 |           3 |      6

原始问题的扩展
但是,运行以下查询 -

SELECT id, category_id, max( rating ) AS max_rating 
FROM `articles`
GROUP BY category_id

结果如下,其中除 id 字段外的所有内容均符合预期。我知道如何使用子查询来做到这一点 - 正如在同一问题中所回答的 - 使用子查询

id  category_id     max_rating
1    1                10
3    2                7
4    3                6

笼统地说
不包括分组列 (category_id) 和计算列(返回聚合函数结果的列,例如 SUM()MAX() 等。 -在本例中为 max_ rating),其他字段中返回的值只是每个分组结果集下的第一行(在本例中按 category_id 分组)。例如,id =1 的记录是表中 category_id 1 下的第一个记录(id 1 和 2 在 category_id 下) code> 1) 所以它被返回。

我只是想知道是否不可能以某种方式克服这种默认行为以根据条件返回行?如果mysql可以对每个分组结果集执行计算(执行MAX()计数等),那么为什么它不能返回与最大评级相对应的行。是否无法在没有子查询的情况下在单个查询中执行此操作?在我看来,这是一个常见的要求。

更新
我也无法从 Naktibalda 的解决方案中弄清楚我想要什么。再次提一下,我知道如何使用子查询来做到这一点,正如 OMG Ponies 再次回答的那样。

Extending further from this question Query to find top rated article in each category -

Consider the same table -

id | category_id | rating
---+-------------+-------
 1 |           1 |     10
 2 |           1 |      8
 3 |           2 |      7
 4 |           3 |      5
 5 |           3 |      2
 6 |           3 |      6

There is a table articles, with fields id, rating (an integer from 1-10), and category_id (an integer representing to which category it belongs). And if I have the same goal to get the top rated articles in each query (this should be the result):-

Desired Result

id | category_id | rating
---+-------------+-------
 1 |           1 |     10
 3 |           2 |      7
 6 |           3 |      6

Extension of original question
But, running the following query -

SELECT id, category_id, max( rating ) AS max_rating 
FROM `articles`
GROUP BY category_id

results into the following where everything, except the id field, is as desired. I know how to do this with a subquery - as answered in the same question - Using subquery.

id  category_id     max_rating
1    1                10
3    2                7
4    3                6

In generic terms
Excluding the grouped column (category_id) and the evaluated columns (columns returning results of aggregate function like SUM(), MAX() etc. - in this case max_rating), the values returned in the other fields are simply the first row under every grouped result set (grouped by category_id in this case). E.g. the record with id =1 is the first one in the table under category_id 1 (id 1 and 2 under category_id 1) so it is returned.

I am just wondering is it not possible to somehow overcome this default behavior to return rows based on conditions? If mysql can perform calculation for every grouped result set (does MAX() counting etc) then why can't it return the row corresponding to the maximum rating. Is it not possible to do this in a single query without a subquery? This looks to me like a frequent requirement.

Update
I could not figure out what I want from Naktibalda's solution too. And just to mention again, I know how to do this using a subquery, as again answered by OMG Ponies.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

情释 2024-10-11 13:29:04

使用:

SELECT x.id,
       x.category_id,
       x.rating
  FROM YOUR_TABLE x
  JOIN (SELECT t.category_id,
               MAX(t.rating) AS max_rating
          FROM YOUR_TABLE t
      GROUP BY t.category_id) y ON y.category_id = x.category_id
                               AND y.max_rating = x.rating

Use:

SELECT x.id,
       x.category_id,
       x.rating
  FROM YOUR_TABLE x
  JOIN (SELECT t.category_id,
               MAX(t.rating) AS max_rating
          FROM YOUR_TABLE t
      GROUP BY t.category_id) y ON y.category_id = x.category_id
                               AND y.max_rating = x.rating
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文