How to update Column related to Row Number

UPDATE contact
SET ContactNumber = (@rownum := 1 + @rownum) + 500
WHERE 0 = (@rownum:=0)
-- for a given client, for example, Client_Id=123, use
-- WHERE Client_Id = 123 + (@rownum:=0)
ORDER BY id;

The syntax for using joins in UPDATE in MySQL and MariaDB is: UPDATE a JOIN b ON ... SET ... WHERE ... ;. The syntax you used UPDATE .. SET ... FROM ... WHERE ... ; is used in SQL Server.

For recent versions of MariaDB that have window/ranking functions, the following will work:

UPDATE
    contact AS m
  JOIN
    ( SELECT Id, row_number() OVER (ORDER BY Id) AS rn 
      FROM contact
    ) AS sub
  ON  m.Id = sub.Id
SET
    m.ContactNumber = sub.rn + 500
 ;

Tested in dbfiddle.uk

Tags:

Mysql

Mariadb