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

How to dynamically modify container port mapping in Docker

Preface: Docker port mapping is often done by map...

Vue+thinkphp5.1+axios to realize file upload

This article shares with you how to use thinkphp5...

Use PHP's mail() function to send emails

Sending emails using PHP's mail function The ...

VMware configuration hadoop to achieve pseudo-distributed graphic tutorial

1. Experimental Environment serial number project...

SQL implements LeetCode (180. Continuous numbers)

[LeetCode] 180. Consecutive Numbers Write a SQL q...

Detailed explanation of the watch listener example in vue3.0

Table of contents Preface The difference between ...

Detailed explanation of Jquery datagrid query

Table of contents Add code to the Tree item; 1. S...

Using Apache ab to perform http performance testing

Mac comes with Apache environment Open Terminal a...

Implementing simple tabs with js

Tab selection cards are used very frequently on r...

Summary of MySQL Architecture Knowledge Points

1. Databases and database instances In the study ...

Example of converting webpack images to base64

Download url-loader yarn add -D url-loader module...

Example code for implementing equal height layout in multiple ways with CSS

The equal height layout described in this article...

CSS tips for controlling animation playback and pause (very practical)

Today I will introduce a very simple trick to con...

Mysql sql slow query monitoring script code example

1. Modify my.cnf #The overall effect is that both...

Analysis of log files in the tomcat logs directory (summary)

Each time tomcat is started, the following log fi...