mysql query to update field to max(field) + 1

前端 未结 2 629
不思量自难忘°
不思量自难忘° 2020-12-06 05:38

What I want to do is:

UPDATE table SET field = MAX(field) + 1 WHERE id IN (1, 3, 5, 6, 8);

The semantics of this statement, in my mind, wou

2条回答
  •  余生分开走
    2020-12-06 06:28

    In order to get around the mysql-error-1093, use a subquery/derived table/inline view:

    UPDATE table
          SET field = (SELECT x.max_field
                              FROM (SELECT MAX(t.field) + 1 AS max_field
                                            FROM TABLE t
                                           WHERE t.id IN (1,3,5,6,8) ) x)
    

提交回复
热议问题