Home > Database > Mysql Tutorial > body text

Why Can\'t I Use LIMIT in an UPDATE Query with Multiple Tables in MySQL?

Linda Hamilton
Release: 2024-10-26 02:50:02
Original
632 people have browsed it

Why Can't I Use LIMIT in an UPDATE Query with Multiple Tables in MySQL?

Incorrect Usage of UPDATE and LIMIT in MySQL Queries

Question:

How can I resolve the error "Incorrect usage of UPDATE and LIMIT" when executing a MySQL query?

Problem:

The following MySQL code raises an error due to incorrect use of UPDATE and LIMIT:

$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";
Copy after login

Answer:

The MySQL documentation for UPDATE states:

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

Therefore, when using the multiple-table syntax for UPDATE, it is not possible to use the LIMIT clause. This restriction is in place to prevent inconsistent updates across multiple tables.

Correction:

To resolve the error, remove the LIMIT clause from the query:

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

The above is the detailed content of Why Can\'t I Use LIMIT in an UPDATE Query with Multiple Tables in MySQL?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!