Skip to content
Advertisement

How to update MySQL rows from 4th row to end of the result? [closed]

For example, I have n rows, I need to update from the 4th row to the nth row. here n means the last row of the query. I know multi-row updates can crash applications but in my case, I am sure that I will have a max of 40 rows for 1 user.

Advertisement

Answer

Please check this query for user wise update where update records start from 4 and onwards. Enable WHERE clause for specific userid otherwise disable it.

-- MySQL (v5.8)
UPDATE test tt
INNER JOIN (SELECT id, userid
                 , ROW_NUMBER() OVER (PARTITION BY userid order by id) row_num
            FROM test
            -- WHERE userid = 1 
           ) t
ON tt.id = t.id
AND t.row_num >= 4
SET tt.amount = tt.amount + 10;

Please check from url https://dbfiddle.uk/?rdbms=mysql_8.0&fiddle=e0cda11163a44f7a2b82e31c6d13ed8d

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement