简体   繁体   English

MySQL REPLACE 多个值

[英]MySQL REPLACE multiple values

I have data in a column that is causing problems.我在导致问题的列中有数据。 There are multiple bad characters I need to remove.我需要删除多个坏字符。 I'd like to do this in the query.我想在查询中执行此操作。

On this question: MySQL string replace关于这个问题: MySQL 字符串替换

I see where I can SELECT REPLACE(string_column, 'search', 'replace') as url but this only works for example replacing a / with //我知道我可以在哪里SELECT REPLACE(string_column, 'search', 'replace') as url但这仅适用于例如将 / 替换为 //

I need to replace / with // and also & with && for example in a single query.我需要将 / 替换为 // 并且将 & 替换为 && 例如在单个查询中。 What is the best way to achieve this?实现这一目标的最佳方法是什么?

If you are replacing multiple character then you need to use multiple replace in one query something as below. 如果要替换多个字符,则需要在一个查询中使用多个替换,如下所示。 But if there are many characters to be replaced then its better to use application layer to handle it. 但是如果要替换许多字符,那么最好使用应用程序层来处理它。 In other words for few replacement its easy to use query but for many character replacement the query really becomes messy and ends up hard to read or change. 换句话说,少数替换它易于使用的查询,但对于许多字符替换,查询真的变得混乱,最终难以阅读或更改。

select
replace(
  replace(string_column,'/','//'),'&','&&'
)

If you are using MySQL Version 8+ then below is the built-in function that might help you better.如果您使用的是MySQL 版本 8+ ,那么下面是内置的 function 可能会更好地帮助您。

String细绳 Replace代替 Output Output
w"w'w. ex%a&m:pl–ec)o(m w"w'w. ex%a&m:pl–ec)o(m "'%&:)(– "'%&:)(– www.example.com www.example.com

MySQL Query: MySQL 查询:

SELECT REGEXP_REPLACE('w"w\'w. ex%a&m:p l–e.c)o(m', '[("\'%[:blank:]]&:–)]', '');

Almost for all bugging characters-几乎所有的窃听角色——

SELECT REGEXP_REPLACE(column, '[\("\'%[[:blank:]]&:–,#$@!;\\[\\]\)<>\?\*\^]+','')

MySQL built-in function ( MySQL version 8+ ): REGEXP_REPLACE (string, patterns, replace-with) MySQL 内置 function( MySQL 版本 8+ ): REGEXP_REPLACE (string, patterns, replace-with)

REGEXP_REPLACE gives you the facility to search multiple characters and replace them in one shot. REGEXP_REPLACE 让您能够搜索多个字符并一次性替换它们。

To find multiple characters, pattern is:要查找多个字符,模式是:

[
    (               -- open parenthesis     
    "               -- double quotes        
    \'              -- single quotes        
    %               -- percentage       
    [:blank:]       -- space                
    &               -- ampersand        
    :               -- colon
    –               -- non ASCII character  
    )               -- close parenthesis        
]

Notice the [:blank:] , this is a [:character_class:] and MySQL does support many names.注意[:blank:] ,这是一个[:character_class:]并且 MySQL 确实支持许多名称。

Character Class Name字符 Class 名称 Meaning意义
alnum Alphanumeric characters字母数字字符
alpha α Alphabetic characters字母字符
blank空白的 Whitespace characters空白字符
cntrl控制 Control characters控制字符
digit数字 Digit characters数字字符
graph图形 Graphic characters图形字符
lower降低 Lowercase alphabetic characters小写字母字符
print打印 Graphic or space characters图形或空格字符
punct点点 Punctuation characters标点符号
space空间 Space, tab, newline, and carriage return空格、制表符、换行符和回车符
upper Uppercase alphabetic characters大写字母字符
xdigit数字 Hexadecimal digit characters十六进制数字字符

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM