Why MySQL UPDATE Fails with AND and How to Fix It
The article explains a common MySQL UPDATE mistake where developers use AND between column assignments, shows the unexpected result of owner_code becoming 0, analyzes the logical expression behind it, and provides the correct comma‑separated syntax to reliably update multiple fields.
Problem
Developers often report that an UPDATE statement executes without error but appears to have no effect on the data.
Incorrect SQL
update apps set owner_code='43212' and owner_name='李四' where owner_code='13245' and owner_name='张三';The statement uses AND between column assignments, which is syntactically accepted but semantically wrong.
Observed Result
Before execution the row contains:
After execution the row shows:
The owner_name value remains unchanged, while owner_code becomes 0, indicating an unexpected side‑effect.
Analysis
MySQL’s UPDATE syntax expects a comma‑separated list of column=value pairs. Using AND turns the expression into a logical operation: ('43212' AND owner_name='李四'). Since the string literal evaluates to true (1) and the comparison owner_name='李四' is false (0), the whole expression evaluates to 0. MySQL stores this boolean 0 in owner_code, which explains the observed result.
Correct Syntax
update apps set owner_code='43212', owner_name='李四' where owner_code='13245' and owner_name='张三';Running the corrected statement updates both columns as intended.
Conclusion
When updating multiple columns in a single MySQL UPDATE statement, separate assignments with commas, not AND. Using AND creates a logical expression that can unintentionally set numeric columns to 0.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Java Architect Essentials
Committed to sharing quality articles and tutorials to help Java programmers progress from junior to mid-level to senior architect. We curate high-quality learning resources, interview questions, videos, and projects from across the internet to help you systematically improve your Java architecture skills. Follow and reply '1024' to get Java programming resources. Learn together, grow together.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
