mysql中replace函数的用法_MySQL
mysql中replace函数的用法_MySQL
##-----MySQL数据替换即replace的运用-----##
#这里主要介绍一些关于replace函数的用法。关于研究这个主要是发现replace功能很强大
#对于屏蔽关键字很有用处。
#现在继续在以前的数据表中进行相关操作。
示例数据表:
mysql> select * from mytable;
+----+--------+-------+
| id | name | count |
+----+--------+-------+
| 1 | 张三 | 1000 |
| 2 | 李四 | 500 |
| 3 | 王老虎 | 100 |
| 4 | 赵大 | 1000 |
| 5 | 王二小 | 500 |
| 6 | 三亚子 | 100 |
| 7 | ?阿琼 | 1000 |
| 8 | 秋水虾 | 500 |
| 22 | 害人精 | 100 |
+----+--------+-------+
9 rows in set (0.00 sec)
#这里有9条原始数据,下面用replace进行数据的替换:
#update mytable set name=replace(name,'害人精','马屁精');
mysql> update mytable set name=replace(name,'张三','阿琼');
Query OK, 1 row affected (0.06 sec)
Rows matched: 9 Changed: 1 Warnings: 0
+----+--------+-------+
| id | name | count |
+----+--------+-------+
| 1 | 阿琼 | 1000 |
| 2 | 李四 | 500 |
#为了更加清晰的了解replace替换的方式,这里我们让其替换多个数据:
#update mytable set name=replace(name,'三','大炮');
#由于数据被更改过,里面只有一个’三‘,这里再添加几条数据:
#insert into mytable(name,count) values ('三毛子','200');
#insert into mytable(name,count) values ('张三胖','250');
mysql> insert into mytable(name,count) values ('三毛子','200');
Query OK, 1 row affected (0.00 sec)
mysql> insert into mytable(name,count) values ('张三胖','250');
Query OK, 1 row affected (0.00 sec)
#现在进行替换:
mysql> update mytable set name=replace(name,'三','大炮');
Query OK, 3 rows affected (0.00 sec) #这里显示改变了三条数据,找数据表中有3天带'三'数据
Rows matched: 11 Changed: 3 Warnings: 0
#结果:
| 6 | 大炮亚子 | 100 |
| 7 | ?阿琼 | 1000 |
| 8 | 秋水虾 | 500 |
| 22 | 马屁精 | 100 |
| 23 | 大炮毛子 | 200 |
| 24 | 张大炮胖 | 250 |
+----+----------+-------+
11 rows in set (0.00 sec)
##多条数据替换
#一开始以为这是个错误的sql语句:
#update mytable set name=replace(name,'马屁精','小马哥'),name=replace(name,'小李子','李子');
mysql> update mytable set name=replace(name,'马屁精','小马哥'),name=replace(name
,'小李子','李子');
Query OK, 2 rows affected (0.00 sec)
Rows matched: 11 Changed: 2 Warnings: 0
#从结果来看,如果进行多组数据的替换可以使用上面的方式。
#接下来进行各个字段的替换:
#update mytable set name=replace(name,'秋水虾','吐丝草'),count=replace(count,100,300);
mysql> update mytable set name=replace(name,'秋水虾','吐丝草'),count=replace(cou
nt,100,300);
Query OK, 7 rows affected (0.00 sec) #影响了7个结果,这确实出乎意料,按照我的预测应该
Rows matched: 11 Changed: 7 Warnings: 0 #为1+3=4个影响结果
#下面我们看看数据是如何变化的:
+----+----------+-------+
| id | name | count |
+----+----------+-------+
| 1 | 阿琼 | 3000 | #1000变为3000也就是说将前三位改变了。
| 2 | 李子 | 500 |
| 3 | 王老虎 | 300 | #100变为300,正常变化
| 4 | 赵大 | 3000 |
| 5 | 王二小 | 500 |
| 6 | 大炮亚子 | 300 |
| 7 | ?阿琼 | 3000 |
| 8 | 吐丝草 | 500 |
| 22 | 小马哥 | 300 |
| 23 | 大炮毛子 | 200 |
| 24 | 张大炮胖 | 250 |
+----+----------+-------+
11 rows in set (0.00 sec)
#对于这样的结果只能看作是在replace中所有的数据都是字符串。(仅个人认为)
#update mytable set name=replace(name,王二小,王铁柱); #该方式无法通过
#update mytable set count=replace(count,'500','100');
mysql> update mytable set count=replace(count,'500','100');
Query OK, 3 rows affected (0.00 sec) #确实为预期结果
Rows matched: 11 Changed: 3 Warnings: 0
#为此,数据在replace中仅仅是字符串
mysql> update mytable set count=replace(count,'300','150');
Query OK, 6 rows affected (0.00 sec) #结果再次得以证明
Rows matched: 11 Changed: 6 Warnings: 0
##综上,replace可以用于一个字段的多个数据替换,也可以用于不同字段的替换。
#在mysql中replace可以在一定范围内起到insert的作用,并且语法与之相似
#replace into mytable(name,count) values ('来福','70'),('力拓','600');
mysql> replace into mytable(name,count) values ('来福','70'),('力拓','600');
Query OK, 2 rows affected (0.00 sec)
Records: 2 Duplicates: 0 Warnings: 0
#如果数据库中有类似数据又该如何呢?
replace into mytable(name,count) values ('来福','70');
mysql> replace into mytable(name,count) values ('来福','70');
Query OK, 1 row affected (0.00 sec)
#这里我们查看一下该表的索引,看来索引为默认的ID.
mysql> show index from mytable;
+---------+------------+----------+--------------+-------------+-----------+----
---------+----------+--------+------+------------+---------+
| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Car
dinality | Sub_part | Packed | Null | Index_type | Comment |
+---------+------------+----------+--------------+-------------+-----------+----
---------+----------+--------+------+------------+---------+
| mytable | 0 | PRIMARY | 1 | id | A |
14 | NULL | NULL | | BTREE | |
+---------+------------+----------+--------------+-------------+-----------+----
---------+----------+--------+------+------------+---------+
1 row in set (0.00 sec)
#根据文档介绍,如果只有一个索引,那么replace相当于insert.
##记得我曾经创建了两个表,现在我们切换到另一个数据较少的表mytab上进行操作演示:
mysql> show tables;
+----------------+
| Tables_in_mydb |
+----------------+
| mytab |
| mytable |
+----------------+
2 rows in set (0.00 sec)
#其默认的索引为id
mysql> show index from mytab;
+-------+------------+----------+--------------+-------------+-----------+------
-------+----------+--------+------+------------+---------+
| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardi
nality | Sub_part | Packed | Null | Index_type | Comment |
+-------+------------+----------+--------------+-------------+-----------+------
-------+----------+--------+------+------------+---------+
| mytab | 0 | PRIMARY | 1 | id | A |
3 | NULL | NULL | | BTREE | |
+-------+------------+----------+--------------+-------------+-----------+------
-------+----------+--------+------+------------+---------+
1 row in set (0.00 sec)
#现在需要向其中添加索引(在这里遇到麻烦了,看来我对于建立索引不怎么会啊,惭愧惭愧)。
#尝试建立了多个索引都是失败,暂时写到这里。。。。
写这些并非上班不务正业,而是公司具体任务被安排到下周,接下来该忙了。
以上就是mysql中replace函数的用法_MySQL的内容,更多相关内容请关注PHP中文网(www.php.cn)!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



In MySQL database, the relationship between the user and the database is defined by permissions and tables. The user has a username and password to access the database. Permissions are granted through the GRANT command, while the table is created by the CREATE TABLE command. To establish a relationship between a user and a database, you need to create a database, create a user, and then grant permissions.

MySQL has a free community version and a paid enterprise version. The community version can be used and modified for free, but the support is limited and is suitable for applications with low stability requirements and strong technical capabilities. The Enterprise Edition provides comprehensive commercial support for applications that require a stable, reliable, high-performance database and willing to pay for support. Factors considered when choosing a version include application criticality, budgeting, and technical skills. There is no perfect option, only the most suitable option, and you need to choose carefully according to the specific situation.

Data Integration Simplification: AmazonRDSMySQL and Redshift's zero ETL integration Efficient data integration is at the heart of a data-driven organization. Traditional ETL (extract, convert, load) processes are complex and time-consuming, especially when integrating databases (such as AmazonRDSMySQL) with data warehouses (such as Redshift). However, AWS provides zero ETL integration solutions that have completely changed this situation, providing a simplified, near-real-time solution for data migration from RDSMySQL to Redshift. This article will dive into RDSMySQL zero ETL integration with Redshift, explaining how it works and the advantages it brings to data engineers and developers.

1. Use the correct index to speed up data retrieval by reducing the amount of data scanned select*frommployeeswherelast_name='smith'; if you look up a column of a table multiple times, create an index for that column. If you or your app needs data from multiple columns according to the criteria, create a composite index 2. Avoid select * only those required columns, if you select all unwanted columns, this will only consume more server memory and cause the server to slow down at high load or frequency times For example, your table contains columns such as created_at and updated_at and timestamps, and then avoid selecting * because they do not require inefficient query se

To fill in the MySQL username and password: 1. Determine the username and password; 2. Connect to the database; 3. Use the username and password to execute queries and commands.

MySQL database performance optimization guide In resource-intensive applications, MySQL database plays a crucial role and is responsible for managing massive transactions. However, as the scale of application expands, database performance bottlenecks often become a constraint. This article will explore a series of effective MySQL performance optimization strategies to ensure that your application remains efficient and responsive under high loads. We will combine actual cases to explain in-depth key technologies such as indexing, query optimization, database design and caching. 1. Database architecture design and optimized database architecture is the cornerstone of MySQL performance optimization. Here are some core principles: Selecting the right data type and selecting the smallest data type that meets the needs can not only save storage space, but also improve data processing speed.

Copy and paste in MySQL includes the following steps: select the data, copy with Ctrl C (Windows) or Cmd C (Mac); right-click at the target location, select Paste or use Ctrl V (Windows) or Cmd V (Mac); the copied data is inserted into the target location, or replace existing data (depending on whether the data already exists at the target location).

Detailed explanation of database ACID attributes ACID attributes are a set of rules to ensure the reliability and consistency of database transactions. They define how database systems handle transactions, and ensure data integrity and accuracy even in case of system crashes, power interruptions, or multiple users concurrent access. ACID Attribute Overview Atomicity: A transaction is regarded as an indivisible unit. Any part fails, the entire transaction is rolled back, and the database does not retain any changes. For example, if a bank transfer is deducted from one account but not increased to another, the entire operation is revoked. begintransaction; updateaccountssetbalance=balance-100wh
