MySQL query to get best Ranks from one table and UPDATE with results another

前端 未结 2 1501
南旧
南旧 2021-01-24 01:25

I have a Ranking board for the best first 100 blogs for each gender Males and Females.

I have a blogs table:

PRIMARY
blogs_id  users_id  blogs_score  blo         


        
2条回答
  •  后悔当初
    2021-01-24 01:43

    I think you should add the a GROUP BY clause this way before ORDER BY

    $sql->query("UPDATE users
                  JOIN (SELECT b.users_id,
                             @curRank := @curRank + 1 AS rank,
                             MAX(b.blogs_score), MAX(b.blogs_score_time   )
                      FROM blogs b
                             JOIN (SELECT @curRank := 0) r
                      GROUP BY b.user_id, rank
                      ORDER BY  b.blogs_score DESC, p.blogs_score_time DESC LIMIT 100
                     ) ranks ON (ranks.users_id = users.id)
                SET users.best_rank = ranks.rank");
    

提交回复
热议问题