MySQL order by before group by

后端 未结 9 2464
梦谈多话
梦谈多话 2020-11-22 06:57

There are plenty of similar questions to be found on here but I don\'t think that any answer the question adequately.

I\'ll continue from the current most popular qu

9条回答
  •  梦谈多话
    2020-11-22 07:40

    Just to recap, the standard solution uses an uncorrelated subquery and looks like this:

    SELECT x.*
      FROM my_table x
      JOIN (SELECT grouping_criteria,MAX(ranking_criterion) max_n FROM my_table GROUP BY grouping_criteria) y
        ON y.grouping_criteria = x.grouping_criteria
       AND y.max_n = x.ranking_criterion;
    

    If you're using an ancient version of MySQL, or a fairly small data set, then you can use the following method:

    SELECT x.*
      FROM my_table x
      LEFT
      JOIN my_table y
        ON y.joining_criteria = x.joining_criteria
       AND y.ranking_criteria < x.ranking_criteria
     WHERE y.some_non_null_column IS NULL;  
    

提交回复
热议问题