MySQL should never write update statements like this

MySQL should never write update statements like this

Preface

Today I will share with you a very classic MySQL "pitfall". MySQL UPDATE statements must not be written like this!

cause

Recently, several developers have asked me on DingTalk, such as the following picture:

The problem can be summarized as follows: when updating a record in MySQL, the syntax is correct, but the record is not updated...

Conclusion: In an UPDATE statement, if you want to update multiple fields, you cannot use "AND" between the fields, but should separate them with commas.

Phenomenon

When I first encountered this problem, I took this statement and executed it directly in the test library. I found that there was indeed a problem, but it was still different from the development description. Here I use test data to simulate it.

The problematic SQL statement:

update apps set owner_code='43212' and owner_name='李四' where 
owner_code='13245' and owner_name='张三';

The record before execution is as follows:

The record after execution is as follows:

As you can see, the result is not as the developer said, "it seems to have no effect", but it actually has an effect:

The value of owner_name did not change, but owner_code became 0!

why? Analysis

It seems that there is no problem with the syntax. I looked up the update syntax in the MySQL official documentation:

Seeing that the format of assignment_list is a comma-separated list of col_name=value, it suddenly became clear that the multi-field update statement that the developer wanted should be written like this:

update apps set owner_code='43212' , owner_name='李四' where 
owner_code='13245' and owner_name='张三'; 

Go back and try again:

Sure enough, this time I got the desired result!

Summary: In an UPDATE statement, if you want to update multiple fields, you cannot use "AND" between the fields, but should separate them with commas.

Postscript: When I had some free time later, I looked back and wondered why the strange result of owner_code=0 appeared when “AND” was used to separate them.

After many attempts, I found that:

update apps set owner_code='43212' and owner_name='李四' where 
owner_code='13245' and owner_name='张三'; 

is equivalent to:

update apps set owner_code=('43212' and owner_name='李四') where 
owner_code='13245' and owner_name='张三'; 

And ('43212' and owner_name='李四') is a logical expression, and it is not difficult to know that owner_name is not '李四'.

Therefore, the result of this logical expression is false, which is equivalent to 0 in MySQL!

Summarize

This is the end of this article about how to never write update statements in MySQL. For more information about writing update statements in MySQL, please search for previous articles on 123WORDPRESS.COM or continue to browse the related articles below. I hope you will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • Detailed usage of mysql update statement
  • Summary of Mysql cross-table update multi-table update sql statement
  • mysql SELECT FOR UPDATE statement usage examples
  • MySQL UPDATE statement detailed explanation
  • PHP+MySQL method to determine whether the update statement is executed successfully
  • Example tutorial on using UPDATE statement in MySQL
  • SQL statement details the correct usage of MySQL update
  • Detailed explanation of MySQL database insert and update statements
  • Detailed explanation of the execution process of mysql update statement
  • Correct use of MySQL update statement

<<:  This article teaches you how to play with CSS border

>>:  HTML Basic Notes (Recommended)

Recommend

Detailed explanation of the use of the clip-path property in CSS

Use of clip-path polygon The value is composed of...

How to add interface listening mask in Vue project

1. Business Background Using a mask layer to shie...

Summary of all HTML interview questions

1. The role of doctype, the difference between st...

MySQL pessimistic locking and optimistic locking implementation

Table of contents Preface Actual Combat 1. No loc...

Implementation code of short video (douyin) watermark removal tool

Table of contents 1. Get the first link first 2. ...

MySQL tutorial data definition language DDL example detailed explanation

Table of contents 1. Introduction to the basic fu...

Detailed explanation of the problems and solutions caused by floating elements

1. Problem Multiple floating elements cannot expa...

Docker time zone issue and data migration issue

Latest solution: -v /usr/share/zoneinfo/Asia/Shan...

Docker implements container port binding local port

Today, I encountered a small problem that after s...