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

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

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

        如何在MySQL中为每个组选择第一行?

        时间:2024-04-16

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

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

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

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

                  问题描述

                  在 C# 中是这样的:

                  In C# it would be like this:

                  table
                     .GroupBy(row => row.SomeColumn)
                     .Select(group => group
                         .OrderBy(row => row.AnotherColumn)
                         .First()
                     )
                  

                  Linq-To-Sql 将其转换为以下 T-SQL 代码:

                  Linq-To-Sql translates it to the following T-SQL code:

                  SELECT [t3].[AnotherColumn], [t3].[SomeColumn]
                  FROM (
                      SELECT [t0].[SomeColumn]
                      FROM [Table] AS [t0]
                      GROUP BY [t0].[SomeColumn]
                      ) AS [t1]
                  OUTER APPLY (
                      SELECT TOP (1) [t2].[AnotherColumn], [t2].[SomeColumn]
                      FROM [Table] AS [t2]
                      WHERE (([t1].[SomeColumn] IS NULL) AND ([t2].[SomeColumn] IS NULL))
                        OR (([t1].[SomeColumn] IS NOT NULL) AND ([t2].[SomeColumn] IS NOT NULL)
                          AND ([t1].[SomeColumn] = [t2].[SomeColumn]))
                      ORDER BY [t2].[AnotherColumn]
                      ) AS [t3]
                  ORDER BY [t3].[AnotherColumn]
                  

                  但它与 MySQL 不兼容.

                  But it is incompatible with MySQL.

                  推荐答案

                  当我写作时

                  SELECT AnotherColumn
                  FROM Table
                  GROUP BY SomeColumn
                  ;
                  

                  它有效.IIRC 在其他 RDBMS 这样的语句中是不可能的,因为不属于分组键的列被引用而没有任何类型的聚合.

                  It works. IIRC in other RDBMS such statement is impossible, because a column that doesn't belongs to the grouping key is being referenced without any sort of aggregation.

                  这种怪癖"与我想要的非常接近.所以我用它来得到我想要的结果:

                  This "quirk" behaves very closely to what I want. So I used it to get the result I wanted:

                  SELECT * FROM 
                  (
                   SELECT * FROM `table`
                   ORDER BY AnotherColumn
                  ) t1
                  GROUP BY SomeColumn
                  ;
                  

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

                  上一篇:MySQL - 获取最低值 下一篇:统计分组返回的记录数

                  相关文章

                    <bdo id='30uGL'></bdo><ul id='30uGL'></ul>
                  1. <small id='30uGL'></small><noframes id='30uGL'>

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

                    <tfoot id='30uGL'></tfoot>
                  2. <legend id='30uGL'><style id='30uGL'><dir id='30uGL'><q id='30uGL'></q></dir></style></legend>