UserID Rank TotalRevenue TotalDishesSold
1550 0 908 45
1141 0 985 67
1402 0 637 34
1063 0 736 89
1204 0 26 78
嗨,这是我第一次使用php进行SQL。根据TotalRevenue然后TotalDishesSold对数据库进行排序的最佳方法/效率是什么,并为每个用户插入排名?我将拥有100k的用户。
答案 0 :(得分:2)
您可以使用ROW_NUMBER()
订购SELECT UserID, ROW_NUMBER() OVER (ORDER BY TotalRevenue DESC, TotalDishesSold DESC),
TotalRevenue, TotalDishesSold
FROM TableName
使用Temp Table存储UserID和Rank。然后加入你的表并更新Rank
临时表
INSERT INTO @Temp (UserID, Rank)
SELECT UserID, ROW_NUMBER() OVER (ORDER BY TotalRevenue DESC, TotalDishesSold DESC)
FROM TableName
更新您的表格
UPDATE T SET
T.Rank = T1.Rank
FROM TableName AS T INNER JOIN
@Temp AS T1 ON T1.UserID = T.UserID