<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中为每个组选择第一行?

        How to select the first row for each group in MySQL?(如何在MySQL中为每个组选择第一行?)

          <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中为每个组选择第一行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  What does SQL clause quot;GROUP BY 1quot; mean?(SQL 子句“GROUP BY 1是什么意思?意思是?)
                  MySQL groupwise MAX() returns unexpected results(MySQL groupwise MAX() 返回意外结果)
                  MySQL SELECT most frequent by group(MySQL SELECT 按组最频繁)
                  Include missing months in Group By query(在 Group By 查询中包含缺失的月份)
                  Why Mysql#39;s Group By and Oracle#39;s Group by behaviours are different(为什么 Mysql 的 Group By 和 Oracle 的 Group by 行为不同)
                  MySQL GROUP BY DateTime +/- 3 seconds(MySQL GROUP BY DateTime +/- 3 秒)
                    <bdo id='30uGL'></bdo><ul id='30uGL'></ul>
                      <tbody id='30uGL'></tbody>
                        • <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>
                        • <legend id='30uGL'><style id='30uGL'><dir id='30uGL'><q id='30uGL'></q></dir></style></legend>