Why MySQL UPDATE Fails with AND and How to Use Comma‑Separated Assignments
A developer noticed an UPDATE statement that appeared to run without effect, discovered that using AND between column assignments is invalid in MySQL, learned the correct comma‑separated syntax, and saw how logical AND expressions can produce unexpected zero values.
Several developers asked why an UPDATE statement that looks syntactically correct does not appear to modify rows in MySQL. The author reproduced the issue in a test database, showing the original record, the executed SQL (shown as an image), and the resulting record after execution (also images). Although the update did affect the data, the developer’s perception was that nothing changed.
Reviewing the MySQL documentation revealed that the assignment_list in an UPDATE must be a comma‑separated list of col_name = value pairs. The problematic statement used the logical operator AND between assignments, which is not valid for updating multiple columns.
The correct syntax, illustrated with an image, uses commas to separate assignments. Re‑running the statement with commas produced the expected result, as confirmed by the before‑and‑after screenshots.
Further investigation explained why using AND yields strange results such as owner_code = 0. In MySQL, an expression like ('43212' AND owner_name='李四') is evaluated as a logical expression; since owner_name='李四' is false, the whole expression evaluates to false, which MySQL treats as 0. An equivalent expression image demonstrates this behavior.
Key takeaway: When updating multiple columns in a single UPDATE statement, separate the assignments with commas, not with the logical AND operator.
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.
Liangxu Linux
Liangxu, a self‑taught IT professional now working as a Linux development engineer at a Fortune 500 multinational, shares extensive Linux knowledge—fundamentals, applications, tools, plus Git, databases, Raspberry Pi, etc. (Reply “Linux” to receive essential resources.)
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.
