简体   繁体   English

MySQL 从 DENSE_RANK() 结果更新相同的表字段

[英]MySQL update same table field from DENSE_RANK() results

i am trying to update field rank using DENSE_RANK() from same table,while trying to execute below query getting "Operand should contain 1 column(s)",kindly help in solving this issue我正在尝试使用同一个表中的 DENSE_RANK() 更新字段排名,同时尝试执行以下查询以获取“操作数应包含 1 列”,请帮助解决此问题

UPDATE scoretable SET rank= (SELECT *,DENSE_RANK() OVER (PARTITION BY game_id ORDER BY points DESC , diff ASC) FROM scoretable WHERE STATUS ='Active')

Table details表详细信息

在此处输入图片说明

DENSE_RANK() query results DENSE_RANK() 查询结果

SELECT *,DENSE_RANK() OVER (PARTITION BY game_id ORDER BY points DESC , diff ASC) FROM scoretable WHERE STATUS ='Active'

在此处输入图片说明

As commentedby P.Salmon: MySQL does not allow the updated table to appear in a subquery.正如 P.Salmon 所评论的:MySQL 不允许更新的表出现在子查询中。 Instead, you can dense_rank() in a subquery, and then join it with the table, like so:相反,您可以在子查询中使用dense_rank() ,然后join其与表连接,如下所示:

update scoretable s
inner join (
    select id, dense_rank() over(partition by game_id order by points desc, diff) rn
    from scoretable
    where status = 'Active'
) r on r.id = s.id
set s.rank = r.rn

Note: this assumes that id is a unique key in the table.注意:这假设id是表中的唯一键。

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

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