MySQL Error: Incorrect usage of UPDATE and LIMIT

后端 未结 5 1137
闹比i
闹比i 2020-11-30 09:42

How can I correct this problem so that my MySQL code works correctly.

Here is my MySQL code that gives me the problem.

$q = \"UPDATE users INNER JOIN         


        
5条回答
  •  悲哀的现实
    2020-11-30 10:20

    I know it is an old question but it is the first link when googling this error. There is a workaround to solve this problem without performance issue (depending on your indexes) by using a derived table.

    UPDATE table1 t1
    JOIN (SELECT t1.id
        FROM table1 t1
        JOIN table2 t2 ON t1.id = t2.id
            AND t2.some_criteria = 'some_value'
        WHERE t1.other_criteria = 'other_value'
        LIMIT 10000
    ) tmp ON tmp.id = t1.id
    SET t1.field_to_update = 'new_value'
    

    Because the LIMIT is inside the subquery, the join will match only the number of rows of the clause LIMIT. So the query will update only those rows.

提交回复
热议问题