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

18 sets of exquisite Apple-style free icon materials to share

Apple Mug Icons and Extras HD StorageBox – add on...

Detailed example of MySQL subquery

Subquery Classification Classification by returne...

Detailed steps to configure MySQL remote connection under Alibaba Cloud

Preface As we all know, by default, the MySQL ins...

3 ways to correctly modify the maximum number of connections in MySQL

We all know that after the MySQL database is inst...

Some lesser-known sorting methods in MySQL

Preface ORDER BY 字段名升序/降序, I believe that everyon...

React implements the sample code of Radio component

This article aims to use the clearest structure t...

Issues with upgrading Python and installing Mongodb drivers under Centos

Check the Python version python -V If it is below...

Set IE8 to use IE7 style code

<meta http-equiv="x-ua-compatible" co...

Ant designing vue table to achieve a complete example of scalable columns

Perfect solution to the scalable column problem o...

Bootstrap 3.0 study notes buttons and drop-down menus

The previous article was a simple review of the B...

Detailed tutorial on installing Python 3.6.6 from scratch on CentOS 7.5

ps: The environment is as the title Install possi...

How are spaces represented in HTML (what do they mean)?

In web development, you often encounter characters...

Tomcat parses XML and creates objects through reflection

The following example code introduces the princip...

Practical record of optimizing MySQL tables with tens of millions of data

Preface Let me explain here first. Many people on...