MySQL query to dynamic “Ranking rows”

后端 未结 2 602
后悔当初
后悔当初 2021-01-15 11:55

I\'m having problems running a query ranking. The inner SELECT gives the rows in order of ranking, for each line, the variable @rank increases, if not a position equal to th

2条回答
  •  醉话见心
    2021-01-15 12:20

    having
    It will be slow, but a having clause will run after all the selects, joins, where and group by's have finished and are fully resolved.
    The only problem is that having does not use an index, whilst where does use an index.

    SELECT
      ranking stuff
    FROM 
      lot of tables
    WHERE simple_condition
    HAVING filters_that_run_last
    

    Make your joins explicit
    Note that you don't have to mix explicit and implicit joins.
    If you want a cross join, you can use the cross join keyword.

        ....
        ) AS Ranking
        CROSS JOIN (SELECT @curr := null, @prev := null, @rank := 0) InitVars
    WHERE
      Ranking.regional_id = 1003
    

提交回复
热议问题