MySQL replace函数替换字符串语句的用法
MySQL replace函数我们经常用到,下面就为您详细介绍MySQL replace函数的用法,希望对您学习MySQL replace函数方面能有所启迪。
MySQL replace函数我们经常用到,下面就为您详细介绍MySQL replace函数的用法,希望对您学习MySQL replace函数方面能有所启迪。最近在研究CMS,在数据转换的时候需要用到mysql的MySQL replace函数,这里简单介绍一下。
比如你要将表 tb1里面的 f1字段的abc替换为def
UPDATE tb1 SET f1=REPLACE(f1, 'abc', 'def');
REPLACE(str,from_str,to_str)
在字符串 str 中所有出现的字符串 from_str 均被 to_str替换,然后返回这个字符串:
mysql> SELECT REPLACE('www.mysql.com', 'w', 'Ww');
-> 'WwWwWw.mysql.com'
这个函数是多字节安全的。
示例:
代码如下:
UPDATE `dede_addonarticle` SET body = REPLACE ( body, '', '' );
UPDATE `dede_addonarticle` SET body = REPLACE ( body, '', '' );
UPDATE `dede_addonarticle` SET body = REPLACE ( body, '
UPDATE `dede_archives` SET title= REPLACE ( title, '大洋新闻 - ', '' );
UPDATE `dede_addonarticle` SET body = REPLACE ( body, '../../../../../../', 'http://sc.jb51.net/meal/' );
mysql replace
用法1.replace intoreplace into table (id,name) values('1','aa'),('2','bb')
此语句的作用是向表table中插入两条记录。
2.replace(object, search,replace)
把object中出现search的全部替换为replaceselect replace('www.163.com','w','Ww')--->WwW wWw.163.com
例:把表table中的name字段中的 aa替换为bbupdate table set name=replace(name,'aa','bb')
下面是其它网友的文章,可以参考下:
今天无意之中发现了replace的用法,并且在项目中还有相关的应用,应用到项目中果然是屡试不爽。朋友们就来看下关于repace的详细解释吧。
REPLACE的运行与INSERT很相像。只有一点除外,如果表中的一个旧记录与一个用于PRIMARY KEY或一个UNIQUE索引的新记录具有相同的值,则在新记录被插入之前,旧记录被删除。请参见13.2.4节,“INSERT语法”。
注意,除非表有一个PRIMARY KEY或UNIQUE索引,否则,使用一个REPLACE语句没有意义。该语句会与INSERT相同,因为没有索引被用于确定是否新行复制了其它的行。
所有列的值均取自在REPLACE语句中被指定的值。所有缺失的列被设置为各自的默认值,这和INSERT一样。您不能从当前行中引用值,也不能在新行中使用值。如果您使用一个例如“SET col_name = col_name + 1”的赋值,则对位于右侧的列名称的引用会被作为DEFAULT(col_name)处理。因此,该赋值相当于SET col_name = DEFAULT(col_name) + 1。
为了能够使用REPLACE,您必须同时拥有表的INSERT和DELETE权限。
REPLACE语句会返回一个数,来指示受影响的行的数目。该数是被删除和被插入的行数的和。如果对于一个单行REPLACE该数为1,则一行被插入,同时没有行被删除。如果该数大于1,则在新行被插入前,有一个或多个旧行被删除。如果表包含多个唯一索引,并且新行复制了在不同的唯一索引中的不同旧行的值,则有可能是一个单一行替换了多个旧行。
受影响的行数可以容易地确定是否REPLACE只添加了一行,或者是否REPLACE也替换了其它行:检查该数是否为1(添加)或更大(替换)。
如果您正在使用C API,则可以使用mysql_affected_rows()函数获得受影响的行数。
目前,您不能在一个子查询中,向一个表中更换,同时从同一个表中选择。
以下是所用算法的更详细的说明(该算法也用于LOAD DATA…REPLACE):
1. 尝试把新行插入到表中
2. 当因为对于主键或唯一关键字出现重复关键字错误而造成插入失败时:
a. 从表中删除含有重复关键字值的冲突行
b. 再次尝试把新行插入到表中
使用格式:
代码如下:
REPLACE [LOW_PRIORITY | DELAYED]
[INTO] tbl_name [(col_name,...)]
VALUES ({expr | DEFAULT},…),(…),…
或:
REPLACE [LOW_PRIORITY | DELAYED]
[INTO] tbl_name
SET col_name={expr | DEFAULT}, …
或:
REPLACE [LOW_PRIORITY | DELAYED]
[INTO] tbl_name [(col_name,...)]
SELECT …

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



MySQL is a commonly used relational database management system that provides a variety of functions to process and operate data. Among them, the REPLACE function is used to replace the specified part of the string. In this article, we will introduce how to use the REPLACE function for string replacement in MySQL and demonstrate its usage through code examples. First, let’s take a look at the syntax of the REPLACE function: REPLACE(str,search_str,replace_str).

What are the string search and replace techniques in Python? (Specific code example) In Python, strings are a common data type, and we often encounter string search and replace operations in daily programming. This article will introduce some common string search and replacement techniques, accompanied by specific code examples. To find a specific substring in a string, you can use the find() method or index() method of the string. The find() method returns the index of the first occurrence of the substring in the string.

PHP is a scripting language widely used in website development and has powerful string processing capabilities. During string processing, sometimes you need to replace multiple texts in the string. In this case, you can use PHP's substr_replace() function to achieve this. The substr_replace() function is used to replace the specified substring in a string and return the replaced string. Its syntax is as follows: stringsubstr_replace(string$str

In PHP development, we often need to replace part of the content in a string. PHP provides a built-in function substr_replace(), which can be used to implement this function. The basic syntax of the substr_replace() function is as follows: substr_replace(string$string,string$replacement,mixed$start[,mixed$length]

Replace text in a string using PHP’s str_replace() function In PHP, the str_replace() function is a very useful function that can replace some text in a string with other text. It's very simple to use, you just need to provide the text to be replaced, the new text to be replaced, and the string to perform the replacement operation. Below we will use specific code examples to demonstrate how to use the str_replace() function for string replacement. First, we define a word

In Java, use the replace() method of the StringBuilder class to replace part of the content in a string. In Java programming, strings are a very important data type, and strings often need to be processed and manipulated. And sometimes we need to replace part of the string to meet our needs. In Java, you can use the replace() method of the StringBuilder class to implement string replacement operations. StringBuilder is a

Use the PHP function "str_replace" to replace specified characters in a string. In PHP, strings are a very common data type, and sometimes certain characters in strings need to be replaced or deleted. For this purpose, PHP provides a very simple function "str_replace" (string replacement) to complete this task. The syntax of str_replace function is as follows: str_replace($search,$replace,$subje

How to use the replaceAll() method of the String class to replace all matching content in a string. Introduction: During string processing, sometimes we need to replace all matching content in a string with specified content. Java provides the replaceAll() method of the String class to implement this function. This article will introduce the use of the replaceAll() method and provide some practical code examples. 1. Basic usage of replaceAll() method repla
