0

我有一个针对各种用户的点击表:

HITS:
id | userid
1  | 1     
2  | 1     
3  | 2     
4  | 1     
5  | 2     
6  | 2     

我希望以最快的方式获取按 ID 排列的这些项目的列表。所以这:

HITS RANKED:
id | userid | ranks
1  | 1      | 1
2  | 1      | 2
3  | 2      | 1
4  | 1      | 3
5  | 2      | 2
6  | 2      | 3

我想避免将两张桌子相互连接,因为当桌子变大时,这需要很长时间。还有其他建议吗?

4

1 回答 1

1
SELECT  ID, 
        UserID, 
        Ranks
FROM
    (
        SELECT  id,
                userid,
                @group:=CASE WHEN @temp <> userid THEN 1 ELSE @group+1 END AS ranks,
                @temp:=userid AS clset
        FROM    (SELECT @group:= 0) s,
                (SELECT @temp:= 0) c,
                (SELECT * FROM hits ORDER BY userid, id) t
    ) x
ORDER   BY ID
于 2013-02-27T08:58:37.863 回答