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

WeChat applet scroll-view realizes left and right linkage

This article shares the specific code for WeChat ...

Detailed explanation of built-in methods of javascript array

Table of contents 1. Array.at() 2. Array.copyWith...

HTML table tag tutorial (20): row background color attribute BGCOLOR

The BGCOLOR attribute can be used to set the back...

HTML+CSS project development experience summary (recommended)

I haven’t updated my blog for several days. I jus...

JS operation object array to achieve add, delete, modify and query example code

1. Introduction Recently, I helped a friend to ma...

Overview of time configuration under Linux system

1. Time types are divided into: 1. Network time (...

How to prevent hyperlink redirection using JavaScript (multiple ways of writing)

Through JavaScript, we can prevent hyperlinks fro...

Steps to deploy ingress-nginx on k8s

Table of contents Preface 1. Deployment and Confi...

How to install multiple mysql5.7.19 (tar.gz) files under Linux

For the beginner's first installation of MySQ...

JavaScript Snake Implementation Code

This article example shares the specific code of ...

Detailed graphic explanation of sqlmap injection

Table of contents 1. We found that this website m...

Convert XHTML CSS pages to printer pages

In the past, creating a printer-friendly version ...

Command to view binlog file creation time in Linux

Table of contents background analyze method backg...

Configure Java development environment in Ubuntu 20.04 LTS

Download the Java Development Kit jdk The downloa...

CSS implements a pop-up window effect with a mask layer that can be closed

Pop-up windows are often used in actual developme...