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

前端 未结 2 1503
南旧
南旧 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

    UPDATE users u, (
    SELECT 
    ( 
        CASE gender 
        WHEN @curType 
        THEN @curRow := @curRow + 1 
        ELSE @curRow := 1 AND @curType := gender END
    ) + 1 AS rank, users_id, blogs_score, blogs_score_time, gender 
    FROM blogs, 
    (SELECT @curRow := 0, @curType := '') r
    ORDER BY gender, blogs_score DESC, blogs_score_time DESC
    ) r1
    SET u.best_rank = r1.rank
    WHERE
    u.id = r1.users_id
    

    It does 2 works:

    1. inner select query generates rank as per gender, score and time
    2. updates user table with respective rank.

提交回复
热议问题