sql,用email替换字符串的一部分

x759pob2  于 2021-06-20  发布在  Mysql
关注(0)|答案(5)|浏览(344)

在my db中,我有一个列user\u email,其值为:

aaa@test.com
bbb@test.com
ccc@test.com

我只想更改@后面的部分电子邮件地址,以便生成的列具有以下值:

aaa@other.net
bbb@other.net
ccc@other.net

我怎么能做到呢?

km0tfn4u

km0tfn4u1#

select concat
        (substring
          ('bbb@test.com',1,char_length
            ('bbb@test.com')-
            char_length
              (substring_index
                ('bbb@test.com','.',-1))),'net') x;

| x            |
| ------------ |
| bbb@test.net |

db fiddle视图

6vl6ewon

6vl6ewon2#

我找到了以下解决方案,似乎可以做到这一点:

UPDATE table_name SET user_email = REPLACE(user_email, '@test.com', '@other.net');
jckbn6z7

jckbn6z73#

使用 SUBSTRING_INDEX 以及 concat 功能

select concat(SUBSTRING_INDEX("aaa@test.com", "@", 1),'@other.net')

输出 aaa@other.net 那你的专栏呢 user_email 会的

select concat(SUBSTRING_INDEX(user_email, "@", 1),'@other.net')
xmd2e60i

xmd2e60i4#

你可以用 replace , substr 以及 instr 合称为:

SELECT replace( 'aaa@test.com',
         substr('aaa@test.com',instr('aaa@test.com','@'),length('aaa@test.com'))
                              ,'@other.net') as result_str;

 result_str
-------------
aaa@other.net

或者从你的table上( tab )有一列称为 email :

select replace(email,substr(email,instr(email,'@'),length(email)),'@other.net') result_str
  from tab;

result_str
-------------
aaa@other.net
bbb@other.net
ccc@other.net

rextester演示

3okqufwl

3okqufwl5#

使用替换功能
演示

select replace(name,substring(name,position('@' in name),length(name)-position('@' in name)+1),'@other.net')

select replace('aaa@test.com',substring('aaa@test.com',position('@' in 'aaa@test.com'),
length('aaa@test.com')-position('@' in 'aaa@test.com')+1),'@other.net')

输出:

val             n
aaa@test.com    aaa@other.net

相关问题