A "classic" pitfall of MySQL UPDATE statement

A "classic" pitfall of MySQL UPDATE statement

Someone asked, for example, the following picture:

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

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:

1. Problematic SQL statements

The record after execution is:

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

why?

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

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:

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:

is equivalent to:

And ( '43212' and owner_name='李四' ) is a logical expression, and it is not difficult to know owner_name is not '李四'. Therefore, the result of this logical expression is false, which is equivalent to 0 in MySQL !

This is the end of this article about a "classic" pitfall of MySQL UPDATE statement. For more relevant MySQL UPDATE content, please search 123WORDPRESS.COM's previous articles or continue to browse the following related articles. I hope everyone will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • Practical MySQL + PostgreSQL batch insert update insertOrUpdate
  • Difference between MySQL update set and and
  • Non-standard implementation code for MySQL UPDATE statement
  • mysql update case update field value is not fixed operation
  • Summary of Mysql update multi-table joint update method
  • Explanation of mysql transaction select for update and data consistency processing

<<:  Docker installation and configuration steps for MySQL

>>:  CSS sample code to achieve circular gradient progress bar effect

Recommend

Automatic line breaks in html pre tags

At this time, you can use overflow:auto; (when the...

JavaScript+html to implement front-end page sliding verification (2)

This article example shares the specific code of ...

Detailed explanation of Socket (TCP) bind from Linux source code

Table of contents 1. A simplest server-side examp...

How to query the minimum available id value in the Mysql table

Today, when I was looking at the laboratory proje...

How to display JSON data in HTML

background: Sometimes we need to display json dat...

Linux directory switching implementation code example

Switching files is a common operation in Linux. W...

Detailed explanation of the usage and differences of MySQL views and indexes

MySQL Views Simply put, a MySQL view is a shortcu...

Java programming to write a JavaScript super practical table plug-in

Table of contents Effects Documentation first ste...

JavaScript message box example

Three types of message boxes can be created in Ja...

Solution to the timeout problem when installing docker-compose with PIP

1: Installation command pip install docker-compos...

An article to teach you HTML

If you are not committed to becoming an artist, t...

HTTP header information interpretation and analysis (detailed summary)

HTTP Header Explanation 1. Accept: Tells the web s...