我想查找排名,然后根据其ID更新其排名列。
我的数据如下(表成员)。
MEMBER_ID | LOAN_AMOUNT | Rank
1 | 2,000.00 | 0
2 | 1,000.00 | 0
3 | 4,000.00 | 0
4 | 1,000.00 | 0
现在我想找到他们的排名并根据它更新排名列。 我的一般查询是这样的:
UPDATE
Member AS dest,
(
SELECT RANK() OVER(ORDER BY t.loan_amount DESC) as [rank],
t.memeber_id,t.loan_amount
FROM Member
) AS src
SET
dest.rank = src.rank
INNER JOIN dest.memeber_id = src.memeber_id
有没有更快的方法来更新我的桌子?
答案 0 :(得分:1)
在mysql中,更新连接的sintax应该为
UPDATE
Member AS dest
INNER JOIN
(
SELECT RANK() OVER(ORDER BY t.loan_amount DESC) as [rank],
t.memeber_id,t.loan_amount
FROM Member
) src ON dest.memeber_id = src.memeber_id
SET dest.rank = src.rank
无论如何,您都应该避免在计算过程中轻松计算存储的数据..在执行选择的同时,也可以轻松获取rank()