Home  >  Article  >  Database  >  Why Does MySQL Throw \"Incorrect Usage of UPDATE and LIMIT\" Error?

Why Does MySQL Throw \"Incorrect Usage of UPDATE and LIMIT\" Error?

Susan Sarandon
Susan SarandonOriginal
2024-10-27 11:20:02611browse

Why Does MySQL Throw

Incorrect Usage of UPDATE and LIMIT

MySQL users may encounter the error "Incorrect usage of UPDATE and LIMIT" when attempting to execute queries involving both the UPDATE statement and the LIMIT clause.

To resolve this issue, it's important to understand the limitations of the UPDATE statement when used with multiple tables. According to the MySQL documentation, "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."

In the example code provided:

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

The query attempts to perform an update using multiple tables (users and contact_info) while also applying a LIMIT clause. As explained earlier, LIMIT cannot be used in such scenarios.

To rectify the issue, consider modifying the query as follows:

$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) . "');";

By removing the LIMIT clause, the query will successfully execute and update the specified row(s) in the users table.

The above is the detailed content of Why Does MySQL Throw \"Incorrect Usage of UPDATE and LIMIT\" Error?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn