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

      <legend id='MEspk'><style id='MEspk'><dir id='MEspk'><q id='MEspk'></q></dir></style></legend>

    1. <tfoot id='MEspk'></tfoot>
          <bdo id='MEspk'></bdo><ul id='MEspk'></ul>

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

        如何在 MySQL 中创建一个连续的组?

        时间:2024-04-16

        <legend id='NvkqK'><style id='NvkqK'><dir id='NvkqK'><q id='NvkqK'></q></dir></style></legend>
            • <bdo id='NvkqK'></bdo><ul id='NvkqK'></ul>

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

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

                • 本文介绍了如何在 MySQL 中创建一个连续的组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  如何返回 MySQL 中有效的连续"GROUP BY.换句话说,一个遵守记录集顺序的 GROUP BY?

                  例如,SELECT MIN(col1), col2, COUNT(*) FROM table GROUP BY col2 ORDER BY col1 来自下表,其中 col1 是唯一的有序索引:

                  <前>1个2个3 乙4 b5个6个

                  返回:

                  1 一个 43 b 2

                  但我需要返回以下内容:

                  1 一个 23 b 25 一 2

                  解决方案

                  使用:

                   SELECT MIN(t.id) 'mi',t.val,数数(*)从(选择 x.id,x.val,案件当 xt.val 为 NULL 或 xt.val != x.val THEN@rownum := @rownum+1别的@rownum结束为 grp从表 x加入(选择@rownum := 0)r左连接(选择 t.id +1 'id',值从表 t) xt ON xt.id = x.id) tGROUP BY t.val, t.grp按米订购

                  这里的关键是创建一个允许分组的人工值.

                  之前,更正了古法的回答:

                   SELECT t.id, t.val从表 t在 t2.id + 1 = t.id 上左连接表 t2哪里 t2.val 为空或 t.val <>t2.val

                  How can I return what would effectively be a "contiguous" GROUP BY in MySQL. In other words a GROUP BY that respects the order of the recordset?

                  For example, SELECT MIN(col1), col2, COUNT(*) FROM table GROUP BY col2 ORDER BY col1 from the following table where col1 is a unique ordered index:

                  1    a
                  2    a
                  3    b
                  4    b
                  5    a
                  6    a

                  returns:

                  1    a    4
                  3    b    2

                  but I need to return the following:

                  1    a    2
                  3    b    2
                  5    a    2

                  解决方案

                  Use:

                     SELECT MIN(t.id) 'mi', 
                            t.val, 
                            COUNT(*)
                       FROM (SELECT x.id, 
                                   x.val, 
                                   CASE 
                                     WHEN xt.val IS NULL OR xt.val != x.val THEN 
                                       @rownum := @rownum+1 
                                     ELSE 
                                       @rownum 
                                   END AS grp
                              FROM TABLE x
                              JOIN (SELECT @rownum := 0) r
                         LEFT JOIN (SELECT t.id +1 'id',
                                           t.val
                                      FROM TABLE t) xt ON xt.id = x.id) t
                   GROUP BY t.val, t.grp
                   ORDER BY mi
                  

                  The key here was to create an artificial value that would allow for grouping.

                  Previously, corrected Guffa's answer:

                     SELECT t.id, t.val
                       FROM TABLE t
                  LEFT JOIN TABLE t2 on t2.id + 1 = t.id
                      WHERE t2.val IS NULL 
                         OR t.val <> t2.val
                  

                  这篇关于如何在 MySQL 中创建一个连续的组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:SQL Server:只有 GROUP BY 中的最后一个条目 下一篇:MYSQL 错误 1045 拒绝访问

                  相关文章

                  • <bdo id='CW2v8'></bdo><ul id='CW2v8'></ul>
                • <legend id='CW2v8'><style id='CW2v8'><dir id='CW2v8'><q id='CW2v8'></q></dir></style></legend>

                    <tfoot id='CW2v8'></tfoot>

                    1. <small id='CW2v8'></small><noframes id='CW2v8'>

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