33

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 contact_info ON contact_info.user_id = users.user_id SET active.users = NULL WHERE (email.contact_info = '" . mysqli_real_escape_string($mysqli, $x) . "' AND active.users = '" . mysqli_real_escape_string($mysqli, $y) . "') LIMIT 1";
$r = mysqli_query ($mysqli, $q) or trigger_error("Query: $q\n<br />MySQL Error: " . mysqli_error($mysqli));
linuxbuild
  • 15,843
  • 6
  • 60
  • 87
HELP
  • 14,237
  • 22
  • 66
  • 100
  • Your query includes references to columns in tables 'active' and 'email', while those tables are not included in the statement. Look at 'http://dev.mysql.com/doc/refman/5.1/en/update.html' for guidance on how to structure a multi-table update. – Martin Nov 27 '10 at 13:28

5 Answers5

56

As per the MySQL docs for UPDATE:

For the multiple-table syntax, UPDATE updates rows in each table named in table_references that satisfy the conditions. In this case, ORDER BY and LIMIT cannot be used.

Mark Amery
  • 143,130
  • 81
  • 406
  • 459
Marc B
  • 356,200
  • 43
  • 426
  • 500
  • 3
    I suppose the docs actually mean to say "neither ORDER BY nor LIMIT can be used" because I attempted LIMIT without using ORDER BY and still encountered this error. – Ted Phillips Mar 23 '21 at 18:32
22

**if you want to update multiple rows using limit in mysql...directly limit you cant use try like this**

UPDATE table_name SET name='test'
     WHERE id IN (
         SELECT id FROM (
             SELECT id FROM table_name 
             ORDER BY id ASC  
             LIMIT 0, 10
         ) tmp
     );
Roopchand
  • 2,608
  • 2
  • 17
  • 20
  • 8
    A subselect within an `IN()` is slow as **** and can lock a table like forever. Use `EXISTS()` instead. – Daniel W. Apr 09 '14 at 16:51
  • Syntax error or access violation: 1235 This version of MySQL doesn't yet support 'LIMIT & IN/ALL /ANY/SOME subquery' – lud May 03 '19 at 10:11
5

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.

Shaolin
  • 415
  • 5
  • 11
4

For the multiple-table syntax, UPDATE updates rows in each table named in table_references that satisfy the conditions. In this case, ORDER BY and LIMIT cannot be used

Beachwalker
  • 7,685
  • 6
  • 52
  • 94
1

@Marc B provides the reason, why update normally can't work with limit.

And @Roopchand also provide a solution.

For people like me, who is trying to avoid turning off the safe update mode

https://stackoverflow.com/a/28316067/1278112
This answer is quite helpful. It give an example

UPDATE customers SET countryCode = 'USA' WHERE country = 'USA'; -- which gives the error, you just write:

UPDATE customers SET countryCode = 'USA' WHERE (country = 'USA' AND customerNumber <> 0); -- Because customerNumber is a primary key you got no error 1175 any more.

And when I face update with the multiple-table syntax, it also worked.

What I want but would raise error code 1175.

UPDATE table1 t1
        INNER JOIN
    table2 t2 ON t1.name = t2.name 
SET 
    t1.column = t2.column
WHERE
    t1.name = t2.name;

The working edition

UPDATE table1 t1
        INNER JOIN
    table2 t2 ON t1.name = t2.name 
SET 
    t1.column = t2.column
WHERE
    (t1.name = t2.name and t1.prime_key !=0);

Which is really simple and elegant. Since the original answer doesn't get too much attention (votes), I post more explanation. Hope this can help others.

Shihe Zhang
  • 2,641
  • 5
  • 36
  • 57