<bdo id='7DoTv'></bdo><ul id='7DoTv'></ul>
  • <i id='7DoTv'><tr id='7DoTv'><dt id='7DoTv'><q id='7DoTv'><span id='7DoTv'><b id='7DoTv'><form id='7DoTv'><ins id='7DoTv'></ins><ul id='7DoTv'></ul><sub id='7DoTv'></sub></form><legend id='7DoTv'></legend><bdo id='7DoTv'><pre id='7DoTv'><center id='7DoTv'></center></pre></bdo></b><th id='7DoTv'></th></span></q></dt></tr></i><div id='7DoTv'><tfoot id='7DoTv'></tfoot><dl id='7DoTv'><fieldset id='7DoTv'></fieldset></dl></div>

    <tfoot id='7DoTv'></tfoot>
    1. <legend id='7DoTv'><style id='7DoTv'><dir id='7DoTv'><q id='7DoTv'></q></dir></style></legend>

      <small id='7DoTv'></small><noframes id='7DoTv'>

      1. MySQL SELECT 按组最频繁

        时间:2024-04-16

            • <bdo id='QrZAL'></bdo><ul id='QrZAL'></ul>

                <legend id='QrZAL'><style id='QrZAL'><dir id='QrZAL'><q id='QrZAL'></q></dir></style></legend><tfoot id='QrZAL'></tfoot>
                  <tbody id='QrZAL'></tbody>
              1. <small id='QrZAL'></small><noframes id='QrZAL'>

                <i id='QrZAL'><tr id='QrZAL'><dt id='QrZAL'><q id='QrZAL'><span id='QrZAL'><b id='QrZAL'><form id='QrZAL'><ins id='QrZAL'></ins><ul id='QrZAL'></ul><sub id='QrZAL'></sub></form><legend id='QrZAL'></legend><bdo id='QrZAL'><pre id='QrZAL'><center id='QrZAL'></center></pre></bdo></b><th id='QrZAL'></th></span></q></dt></tr></i><div id='QrZAL'><tfoot id='QrZAL'></tfoot><dl id='QrZAL'><fieldset id='QrZAL'></fieldset></dl></div>
                  本文介绍了MySQL SELECT 按组最频繁的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  如何获取 MySQL 中每个标签最常出现的类别?理想情况下,我想模拟一个聚合函数来计算 mode列.

                  How do I get the most frequently occurring category for each tag in MySQL? Ideally, I would want to simulate an aggregate function that would calculate the mode of a column.

                  SELECT 
                    t.tag 
                    , s.category 
                  FROM tags t 
                  LEFT JOIN stuff s 
                  USING (id) 
                  ORDER BY tag;
                  
                  +------------------+----------+
                  | tag              | category |
                  +------------------+----------+
                  | automotive       |        8 |
                  | ba               |        8 |
                  | bamboo           |        8 |
                  | bamboo           |        8 |
                  | bamboo           |        8 |
                  | bamboo           |        8 |
                  | bamboo           |        8 |
                  | bamboo           |       10 |
                  | bamboo           |        8 |
                  | bamboo           |        9 |
                  | bamboo           |        8 |
                  | bamboo           |       10 |
                  | bamboo           |        8 |
                  | bamboo           |        9 |
                  | bamboo           |        8 |
                  | banana tree      |        8 |
                  | banana tree      |        8 |
                  | banana tree      |        8 |
                  | banana tree      |        8 |
                  | bath             |        9 |
                  +-----------------------------+
                  

                  推荐答案

                  SELECT t1.*
                  FROM (SELECT tag, category, COUNT(*) AS count
                        FROM tags INNER JOIN stuff USING (id)
                        GROUP BY tag, category) t1
                  LEFT OUTER JOIN 
                       (SELECT tag, category, COUNT(*) AS count
                        FROM tags INNER JOIN stuff USING (id)
                        GROUP BY tag, category) t2
                    ON (t1.tag = t2.tag AND (t1.count < t2.count 
                        OR t1.count = t2.count AND t1.category < t2.category))
                  WHERE t2.tag IS NULL
                  ORDER BY t1.count DESC;
                  

                  <小时>

                  我同意这对于单个 SQL 查询来说太过分了.任何在子查询中使用 GROUP BY 都会让我畏缩.您可以使用视图使其看起来更简单:


                  I agree this is kind of too much for a single SQL query. Any use of GROUP BY inside a subquery makes me wince. You can make it look simpler by using views:

                  CREATE VIEW count_per_category AS
                      SELECT tag, category, COUNT(*) AS count
                      FROM tags INNER JOIN stuff USING (id)
                      GROUP BY tag, category;
                  
                  SELECT t1.*
                  FROM count_per_category t1
                  LEFT OUTER JOIN count_per_category t2
                    ON (t1.tag = t2.tag AND (t1.count < t2.count 
                        OR t1.count = t2.count AND t1.category < t2.category))
                  WHERE t2.tag IS NULL
                  ORDER BY t1.count DESC;
                  

                  但它基本上在幕后做同样的工作.

                  But it's basically doing the same work behind the scenes.

                  您评论说您可以在应用程序代码中轻松执行类似的操作.那你为什么不这样做呢?执行更简单的查询以获取每个类别的计数:

                  You comment that you could do a similar operation easily in application code. So why don't you do that? Do the simpler query to get the counts per category:

                  SELECT tag, category, COUNT(*) AS count
                  FROM tags INNER JOIN stuff USING (id)
                  GROUP BY tag, category;
                  

                  并在应用程序代码中对结果进行排序.

                  And sort through the result in application code.

                  这篇关于MySQL SELECT 按组最频繁的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:在 Group By 查询中包含缺失的月份 下一篇:MySQL groupwise MAX() 返回意外结果

                  相关文章

                  <tfoot id='idZdr'></tfoot>
                    1. <legend id='idZdr'><style id='idZdr'><dir id='idZdr'><q id='idZdr'></q></dir></style></legend>

                      <i id='idZdr'><tr id='idZdr'><dt id='idZdr'><q id='idZdr'><span id='idZdr'><b id='idZdr'><form id='idZdr'><ins id='idZdr'></ins><ul id='idZdr'></ul><sub id='idZdr'></sub></form><legend id='idZdr'></legend><bdo id='idZdr'><pre id='idZdr'><center id='idZdr'></center></pre></bdo></b><th id='idZdr'></th></span></q></dt></tr></i><div id='idZdr'><tfoot id='idZdr'></tfoot><dl id='idZdr'><fieldset id='idZdr'></fieldset></dl></div>
                        <bdo id='idZdr'></bdo><ul id='idZdr'></ul>

                      <small id='idZdr'></small><noframes id='idZdr'>