简体   繁体   English

使用SQL Server存储过程更新用户排名

[英]Updating the user ranking with a SQL Server stored procedure

I have two tables, one contains user data and the other contains user ranking information (points needed for the promotion) 我有两个表,一个包含用户数据,另一个包含用户排名信息(促销所需的积分)

Let's say that the user table looks like this: 假设用户表如下所示:

login | ArticlePoints | PhotoPoints | StageId

and the user ranking information table looks like this: 用户排名信息表如下所示:

StageId | StageName | MinimumPoints

and the user information table might contain data like this: 并且用户信息表可能包含以下数据:

1 | Beginner | 100
2 | Advanced | 200
3 | Expert   | 300

What I would like to have is a procedure which does add user points and check whether it is enough for the ranking promotion. 我想要的是一个添加用户点并检查是否足以满足排名提升的过程。 Right now I do it like this: 现在我这样做:

  1. I do have a function which does check "manually" whether the user points is between 100 and 200 and then it does set the user stage = 2, id it's more it check whether it's between 200 and 300 etc. 我确实有一个功能,可以“手动”检查用户点数是否在100和200之间,然后将用户阶段设置为2,id更重要的是,它检查用户点是否在200和300之间,等等。
  2. Stored procedure which does update users set stage = MYFUNCTION from the point 1 . update users set stage = MYFUNCTION from the point 1 ,确实update users set stage = MYFUNCTION from the point 1存储过程update users set stage = MYFUNCTION from the point 1

The thing is that it's not a good solution, right now it is not ready for the easy updates(I can't just add Super Expert with minimum 400 points, I'd need to edit the function). 事实是,这不是一个好的解决方案,目前还没有准备好进行简单的更新(我不能只添加至少400点的Super Expert,而是需要编辑该函数)。

I am trying to prepare a better solution for this problem but I have no idea how to "connect" both tables. 我正在尝试为这个问题准备更好的解决方案,但是我不知道如何“连接”两个表。

Write an UPDATE query that returns the StageID for the calculated values, something like: 编写一个UPDATE查询,返回计算值的StageID ,例如:

UPDATE t1
SET t1.StageID = 
        (SELECT TOP 1 StageID 
         FROM [RANKING_TABLE] t2 
         WHERE t1.ArticlePoints + t1.PhotoPoints >= t2.MinimumPoints 
         ORDER BY t2.MinimumPoints DESC)
FROM [USER_TABLE] t1

So if the USER has 250 points in total, Beginner and Advanced would be achieved, using the TOP 1 and the ORDER BY t2.MinimumPoints DESC , would select the highest Stage. 因此,如果用户总共有250分,则将使用TOP 1ORDER BY t2.MinimumPoints DESC达到初学者和高级ORDER BY t2.MinimumPoints DESC将选择最高的Stage。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM