我们可以在更新记录时在sql的replace函数中使用字符串函数

时间:2016-07-25 10:49:58

标签: mysql sql replace concat

我想在某些条件下更新我的记录。

例如。我有一个专栏ResidenceAddress1,其中包含电子邮件ID和地址。

示例数据:

sap3200@gmail.com,Rourkela   
sap3212@gmail.com 2nd street,7 hills    
2nd street, sap3212@gmail.com

我通过这种方式找到ResidenceAddress1的电子邮件:

select (concat(trim(substring_index(substring_index(Residence_Address1, '@', '1'), ' ', -1)),'','@gmail.com') as mail,Residence_Address1
from mytable
where Residence_Address1 like '%gmail%' and Email_Personal1=""  

当我以这种方式更新时:

update mytable
set email_Personal1=concat(trim(substring_index(substring_index(Residence_Address1, '@', '1'), ' ', -1)), '','@gmail.com') , 
                 Residence_Address1=replace(residence_address1,"'concat(trim(substring_index(substring_index(Residence_Address1, '@', '1'), ' ', -1)), '','@gmail.com')'",'')
where Residence_Address1 like '%gmail%' and Email_Personal1=""

这样,它会更新email_personal1,但是没有使用替换的空值而不是该行中的gmail id值更新residence_address1。我在哪里误会?

2 个答案:

答案 0 :(得分:1)

您可以在update中更新多个列并混合字符串函数。在这个问题中,你有额外的双引号,这是不需要的。也许你打算这样的事情:

update fulldata_table
    set email_Personal1 = concat(trim(substring_index(substring_index(Residence_Address1, '@', '1'
                                       ), ' ', -1)
       ), '', '@gmail.com'), 
        Residence_Address1 = replace(residence_address1, concat(trim(substring_index(substring_index(Residence_Address1, '@', '1'), ' ', -1)), '', '@gmail.com', '')
    where Residence_Address1 like '%gmail%' and Email_Personal1 = ''

答案 1 :(得分:1)

试试这个:

UPDATE fulldata_table SET
  email_Personal1 = concat(trim(substring_index(substring_index(Residence_Address1, '@', '1'),' ',-1 ) ), '@gmail.com') ,
  Residence_Address1 = replace( residence_address1, concat( trim( substring_index( substring_index(Residence_Address1, '@', '1'), ' ', -1 ) ), '@gmail.com', '' ), '' )
WHERE Residence_Address1 LIKE '%gmail%' AND Email_Personal1 = '';