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
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
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. Instead, you can dense_rank()
in a subquery, and then join
it with the table, like so:
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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.