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

How to point the target link of a tag to iframe

Copy code The code is as follows: <iframe id=&...

Detailed explanation of the process of modifying Nginx files in centos7 docker

1. Install nginx in docker: It is very simple to ...

Convert XHTML CSS pages to printer pages

<br />In the past, creating a printer-friend...

How to implement an array lazy evaluation library in JavaScript

Table of contents Overview How to achieve it Spec...

Mybatis implements SQL query interception and modification details

Preface One of the functions of an interceptor is...

Detailed explanation of small state management based on React Hooks

Table of contents Implementing state sharing base...

MySQL detailed explanation of isolation level operation process (cmd)

Read uncommitted example operation process - Read...

How to install Apache service in Linux operating system

Download link: Operating Environment CentOS 7.6 i...

Native JS to implement hover drop-down menu

JS implements a hover drop-down menu. This is a s...

A brief analysis of SQL examples for finding uncommitted transactions in MySQL

A long time ago, I summarized a blog post titled ...

Detailed comparison of Ember.js and Vue.js

Table of contents Overview Why choose a framework...

Pure CSS to achieve click to expand and read the full text function

Note When developing an article display list inte...