Top N Per Group with Multiple Table Joins

前端 未结 3 924
礼貌的吻别
礼貌的吻别 2020-12-15 12:54

Based on my research, this is a very common problem which generally has a fairly simple solution. My task is to alter several queries from get all results into

3条回答
  •  抹茶落季
    2020-12-15 13:17

    I like this elegant solution, however when I run an adapted but similar query on my dev machine I get a non-deterministic result-set returned. I believe this is due to the way the MySql optimiser deals with assigning and reading user variables within the same statement.

    From the docs:

    As a general rule, you should never assign a value to a user variable and read the value within the same statement. You might get the results you expect, but this is not guaranteed. The order of evaluation for expressions involving user variables is undefined and may change based on the elements contained within a given statement; in addition, this order is not guaranteed to be the same between releases of the MySQL Server.

    Just adding this note here in case someone else comes across this weird behaviour.

提交回复
热议问题