简体   繁体   English

sql 代码显示每个类别的最大值,该类别中有多个最大值?

[英]sql code to display max value from each category having more than one max value in that category?

i have table => post我有表 => 发布

     content || category || views
   -------------------------------
     text 1  |  cat2     | 1000
     text 2  |  cat2     | 1000
     text 3  |  cat1     | 500
     text 4  |  cat1     | 400

i want to display max value from each category but it is showing as:我想显示每个类别的最大值,但它显示为:

     content || category || views
   -------------------------------
     text 1  |  cat2     | 1000
     text 2  |  cat2     | 1000
     text 3  |  cat1     | 500

my desire output:我的愿望 output:

     content || category || views
   -------------------------------
     text 1  |  cat2     | 1000
     text 3  |  cat1     | 500

Basically i want to display only one max value from each category even in case where it has more than one max value.In case if there is more than on max values then i want it to show that value which is first in ordering by content column coln ie text 1.基本上我只想显示每个类别中的一个最大值,即使它有多个最大值。如果有多个最大值,那么我希望它显示按内容列排序的第一个值coln 即文本 1。

query i run:我运行的查询:

SELECT t1.*
FROM post t1
INNER JOIN
(
    SELECT category, MAX(views) AS max_views
    FROM post
    GROUP BY category
) t2
    ON t1.category = t2.category AND t1.views = t2.max_views order by views DESC

There are various ways to do this. 有多种方法可以做到这一点。 If you have a unique id for each row, then you can do: 如果每一行都有唯一的ID,则可以执行以下操作:

SELECT p.*
FROM post p
WHERE p.id = (SELECT p2.id
              FROM post p2
              WHERE p2.category = p.category
              ORDER BY p2.views DESC
              LIMIT 1
             );

It is preferable to do this with a primary key, if you have one. 如果有主键,最好使用主键。 The above query should be quite fast if you have an index on post(category, views) . 如果您对post(category, views)有一个索引,那么上面的查询应该很快。

You can use content if it is unique: 您可以使用content ,如果它是独一无二的:

SELECT p.*
FROM post p
WHERE p.content = (SELECT p2.content
                   FROM post p2
                   WHERE p2.category = p.category
                   ORDER BY p2.views DESC
                   LIMIT 1
                  );

Otherwise, some sort of multiple aggregation comes into play, such as: 否则,多种聚合将起作用,例如:

SELECT p.category, MAX(p.content) as content, MAX(p.views) as max_views
FROM post p INNER JOIN
     (SELECT category, MAX(views) AS max_views
      FROM post
      GROUP BY category
     ) p2
    ON p.category = p2.category AND p.views = p2.max_views GROUP BY p.category;

Of course, MySQL 8+ solves this by implementing the ANSI-standard ROW_NUMBER() functionality. 当然,MySQL 8+通过实现ANSI标准的ROW_NUMBER()功能来解决此问题。

I note that your queries use the generic t as a table alias. 我注意到您的查询使用通用t作为表别名。 That is a bad idea. 那是个坏主意。 You should be using an abbreviation for the table name. 您应该使用表名的缩写。 It makes the queries much easier to follow. 它使查询更容易遵循。

You can get the most viewed content in each category by selecting them in a subquery, along with the max view count. 您可以通过在子查询中选择每个类别中获得最多观看次数的内容,以及最大观看次数。

select distinct t1.category,
    (select t2.content
    from post t2
    where t2.category = t1.category
    order by t2.views
    limit 1
    ) best_content,
    max(t1.views) views
from post t1
group by t1.category

I hope it helps 希望对您有所帮助

A simple solution can be the Use of max function with group by as follows:一个简单的解决方案是将 max function 与 group by 一起使用,如下所示:

SELECT content ,  category, max(views)  FROM post WHERE GROUP by category ;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM