gpt4 book ai didi

mysql - MySQL可以替换多个字符吗?

转载 作者:行者123 更新时间:2023-11-30 21:29:03 25 4
gpt4 key购买 nike

我正在尝试替换 MySQL 字段中的一堆字符。我知道 REPLACE 函数,但它一次只能替换一个字符串。我看不到任何合适的功能 in the manual .

我可以一次替换或删除多个字符串吗?例如,我需要用破折号替换空格并删除其他标点符号。

最佳答案

您可以链接 REPLACE 函数:

select replace(replace('hello world','world','earth'),'hello','hi')

这将打印hi earth

您甚至可以使用子查询来替换多个字符串!

select replace(london_english,'hello','hi') as warwickshire_english
from (
select replace('hello world','world','earth') as london_english
) sub

或者使用 JOIN 替换它们:

select group_concat(newword separator ' ')
from (
select 'hello' as oldword
union all
select 'world'
) orig
inner join (
select 'hello' as oldword, 'hi' as newword
union all
select 'world', 'earth'
) trans on orig.oldword = trans.oldword

我将使用公用表表达式进行翻译,作为读者的练习;)

关于mysql - MySQL可以替换多个字符吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57286853/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com