MySQL 错误:UPDATE 和 LIMIT 的使用不正确

发布于 2024-10-04 21:36:18 字数 472 浏览 2 评论 0原文

我怎样才能纠正这个问题,以便我的 MySQL 代码正常工作。

这是我的 MySQL 代码,它给了我这个问题。

$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));

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));

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

虫児飞 2024-10-11 21:36:18

根据 更新 的 MySQL 文档:

对于多表语法,UPDATE 更新 table_references 中指定的每个表中满足条件的行。在这种情况下,不能使用 ORDER BY 和 LIMIT。

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.

千仐 2024-10-11 21:36:18

**如果你想在mysql中使用limit来更新多行...直接limit你不能像这样使用try**

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
     );

**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
     );
比忠 2024-10-11 21:36:18

我知道这是一个老问题,但它是谷歌搜索此错误时的第一个链接。
有一种解决方法可以通过使用派生表来解决此问题,而不会出现性能问题(取决于您的索引)。

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'

由于 LIMIT 位于子查询内部,因此联接将仅匹配子句 LIMIT 的行数。因此查询将仅更新这些行。

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.

带上头具痛哭 2024-10-11 21:36:18

对于多表语法,UPDATE 更新名为的每个表中的行
满足条件的table_references。在本例中,ORDER BYLIMIT
无法使用

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

辞慾 2024-10-11 21:36:18

@Marc B 提供了 update 通常无法与 limit 一起使用的原因。

@Roopchand 也提供了一个解决方案。

对于像我这样试图避免关闭安全更新模式的人

https://stackoverflow.com /a/28316067/1278112
这个答案非常有帮助。它举了一个例子

更新客户 SET CountryCode = 'USA'
WHERE 国家 = '美国'; -- 这给出了错误,你只需编写:

更新客户 SET CountryCode = 'USA'
WHERE (国家 = '美国' AND customerNumber <> 0); -- 因为 customerNumber 是主键,所以不再出现错误 1175。

当我面对使用多表语法的 update 时,它也起作用了。

我想要什么,但会引发错误代码 1175。

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

工作版本

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);

非常简单而优雅。由于原始答案没有得到太多关注(投票),因此我发布了更多解释。希望这可以帮助其他人。

@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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文