Several practical scenarios for implementing the replace function in MySQL

Several practical scenarios for implementing the replace function in MySQL

REPLACE Syntax

REPLACE(String,from_str,to_str)

That is, replace all occurrences of from_str in String with to_str. Here, from_str does not support regular expression matching.

Operation Example

The test table data is as follows:

mysql> select * from `LOL`;
+----+----------------+--------------+-------+
| id | hero_title | hero_name | price |
+----+----------------+--------------+-------+
| 1 | Blade Shadow | Talon | 6300 |
| 2 | Swift Scout | Teemo | 6300 |
| 3 | Radiant Lady AA | Lux | 1350 |
| 4 | Clockwork A, Summon A | Orianna | 6300 |
| 5 | Supreme Fist | Lee Sin | 6300 |
| 6 | Wuji Sword Master | Easy | 450 |
| 7 | Swift Blade Master | Yasuo | 6300 |
+----+----------------+--------------+-------+
7 rows in set (0.00 sec)

Usage scenario 1 – (Query statement):

Requirement: Replace all "之" in the "hero_title" field with " - " to display. The SQL is as follows:

SELECT REPLACE(hero_title,'之',' - ')as repl_title,hero_name,price from LOL;
mysql> SELECT REPLACE(hero_title,'之',' - ') as repl_title,hero_name,price from `LOL`;
+----------------+--------------+-------+
| repl_title | hero_name | price |
+----------------+--------------+-------+
| Blade - Shadow | Talon | 6300 |
| Swift Scout | Teemo | 6300 |
| Radiant Lady AA | Lux | 1350 |
| Clockwork A, Genie A | Orianna | 6300 |
| Supreme - Fist | Lee Sin | 6300 |
| Wuji Sword Master | Easy | 450 |
| Swift Blade Master | Yasuo | 6300 |
+----------------+--------------+-------+
7 rows in set (0.00 sec)

Usage scenario 2 – (update statement):

Requirement: Remove all "A" in the "hero_title" field. The SQL is as follows:

UPDATE LOL SET hero_title=REPLACE(hero_title,'A','');

mysql> UPDATE `LOL` SET hero_title=REPLACE(hero_title,'A','');
Query OK, 2 rows affected (0.05 sec)
Rows matched: 7 Changed: 2 Warnings: 0
-- Remove and query:
+----+--------------+--------------+-------+
| id | hero_title | hero_name | price |
+----+--------------+--------------+-------+
| 1 | Blade Shadow | Talon | 6300 |
| 2 | Swift Scout | Teemo | 6300 |
| 3 | Radiant Lady | Lux | 1350 |
| 4 | Clockwork Genie | Orianna | 6300 |
| 5 | Supreme Fist | Lee Sin | 6300 |
| 6 | Wuji Sword Master | Easy | 450 |
| 7 | Swift Blade Master | Yasuo | 6300 |
+----+--------------+--------------+-------+
7 rows in set (0.00 sec)

Use scenario 3 – (Replace or insert: REPLACE INTO)

If we want to insert a new record (INSERT), but if the record already exists, delete the original record first and then insert the new record.

Demand scenario:

This table stores the most recent transaction order information for each customer. It is required to ensure that the data of a single user is not entered repeatedly, and to have the highest execution efficiency, the least interaction with the database, and support high availability of the database.
At this time, you can use the "REPLACE INTO" statement, so you don't have to query first and then decide whether to delete and then insert.

  • The "REPLACE INTO" statement determines whether a uniqueness (existence) is based on a unique index or primary key.
  • The "REPLACE INTO" statement determines whether a uniqueness (existence) is based on a unique index or primary key.
  • The "REPLACE INTO" statement determines whether a uniqueness (existence) is based on a unique index or primary key.

Note: As shown in the following SQL, you need to create a unique index (Unique) on the username field and set the transId to auto-increment.

-- 20 points recharge REPLACE INTO last_transaction (transId,username,amount,trans_time,remark) 
  VALUES (null, 'chenhaha', 30, '2020-06-11 20:00:20', 'Member Recharge');
 
-- Buy skin at 21 o'clock REPLACE INTO last_transaction (transId, username, amount, trans_time, remark) 
  VALUES (null, 'chenhaha', 100, '2020-06-11 21:00:00', 'Purchase Lee Sin Supreme Fist skin');

If the record with username='chenhaha' does not exist, the REPLACE statement will insert a new record (first recharge), otherwise, the current record with username='chenhaha' will be deleted and then the new record will be inserted.

Do not give a specific value for id, otherwise it will affect SQL execution, unless there are special business requirements.

This concludes this article about several practical scenarios for implementing the replace function in MySQL. For more relevant content on practical scenarios of MySQL replace, please search for previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope you will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • mysql replace part of the field content and mysql replace function replace()
  • Solution to the problem of data loss when using Replace operation in MySQL
  • Explanation of the usage of replace and replace into in MySQL
  • Detailed explanation of replace into example in mysql
  • Analysis of the usage of replace and regexp for regular expression replacement in MySQL
  • Use replace in mysql to replace part of the content of a field
  • Detailed explanation of the usage of replace into statement in MySQL
  • A brief analysis of the usage of MySQL replace into
  • Detailed explanation of the use of replace syntax in MYSQL batch replacement

<<:  Detailed discussion of several methods for deduplicating JavaScript arrays

>>:  Example code for text origami effect using CSS3

Recommend

2 reasons why html-css tag style setting does not work

1 CSS style without semicolon ";" 2 Tags...

MySQL Router implements MySQL read-write separation

Table of contents 1. Introduction 2. Configure My...

Zabbix configures DingTalk's alarm function with pictures

Implementation ideas: First of all, the alarm inf...

Import csv file into mysql using navicat

This article shares the specific code for importi...

MySQL Series 14 MySQL High Availability Implementation

1. MHA ​By monitoring the master node, automatic ...

Summary of common optimization operations of MySQL database (experience sharing)

Preface For a data-centric application, the quali...

MySQL replication table details and example code

MySQL replication table detailed explanation If w...

js implements clock component based on canvas

Canvas has always been an indispensable tag eleme...

js implements axios limit request queue

Table of contents The background is: What will ha...

CSS Skills Collection - Classics among Classics

Remove the dotted box on the link Copy code The co...

Solve the problem that Docker cannot ping the host machine under Mac

Solution Abandon the Linux virtual machine that c...

The shortest JS to determine whether it is IE6 (IE writing method)

Commonly used JavaScript code to detect which ver...

MySQL 5.7.23 installation and configuration graphic tutorial

This article records the detailed installation pr...