简体   繁体   中英

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

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.

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