MySQL 错误:UPDATE 和 LIMIT 的使用不正确
我怎样才能纠正这个问题,以便我的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
根据 更新 的 MySQL 文档:
As per the MySQL docs for UPDATE:
**如果你想在mysql中使用limit来更新多行...直接limit你不能像这样使用try**
**if you want to update multiple rows using limit in mysql...directly limit you cant use try like this**
我知道这是一个老问题,但它是谷歌搜索此错误时的第一个链接。
有一种解决方法可以通过使用派生表来解决此问题,而不会出现性能问题(取决于您的索引)。
由于 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.
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.
对于多表语法,
UPDATE
更新名为的每个表中的行满足条件的table_references。在本例中,
ORDER BY
和LIMIT
无法使用
For the multiple-table syntax,
UPDATE
updates rows in each table named intable_references that satisfy the conditions. In this case,
ORDER BY
andLIMIT
cannot be used
@Marc B 提供了
update
通常无法与limit
一起使用的原因。@Roopchand 也提供了一个解决方案。
对于像我这样试图避免关闭
安全更新模式
的人https://stackoverflow.com /a/28316067/1278112
这个答案非常有帮助。它举了一个例子
当我面对使用多表语法的
update
时,它也起作用了。我想要什么,但会引发错误代码 1175。
工作版本
非常简单而优雅。由于原始答案没有得到太多关注(投票),因此我发布了更多解释。希望这可以帮助其他人。
@Marc B provides the reason, why
update
normally can't work withlimit
.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
And when I face
update
with the multiple-table syntax, it also worked.What I want but would raise error code 1175.
The working edition
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.