Skip to content
Advertisement

Update Selected Column using ORDER BY or base on Higher Value of Other Column

I would like to make a SQL script or statement which has a function of updating the column but it only update the high OnlineTime.

  • DatabaseName: Ranking
  • Table: ChaInfo
  • Columns: UserNum, CharacterName,OnlineTime,Points

Lets say there are 3 column that has the same UserNum but different OnlineTime Record, Then What I want to happen is UPDATE the POINTS of the one that has a highest OnlineTime.

Sample code but its not working:

UPDATE ChaInfo
SET Points = Points + 10
SELECT TOP 1 UserNum
WHERE UserNum = 1 
ORDER BY OnlineTime DESC

Please help me to fix this code.Thanks.

Advertisement

Answer

If you want to update row with UserNum=1 and highest onlineTime , try this:

UPDATE chainfo
SET points = points + 10
WHERE usernum =1
and onlinetime=(select max(onlinetime)
               from chainfo
               where usernum =1)
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement