在mysql中从一列中删除子字符串

qfe3c7zg  于 2021-06-20  发布在  Mysql
关注(0)|答案(1)|浏览(320)

我想从表的列中删除子字符串的一部分
例如,我的结构如下:

id | Col1                                      
____________________________________________________________
1  | The string I want and the string I don't

我需要更新我的表以便:

id | Col1                                       
____________________________________________________________
1  | The string I want

我尝试使用以下代码:

$var = "UPDATE mytable
  SET col1 = replace(col1,col1(SUBSTRING(col1,19),'')";

但这行不通!

qf9go6mv

qf9go6mv1#

尝试此查询以分隔地址:

UPDATE mytable as a 
INNER JOIN
    (SELECT 
        id,
        Address1, 
        LEFT(Address1, 40 -1) as newCol1,
        substring(Address1, 40) as newCol2
    FROM mytable 
    where length(Address1) > 40) b 
on a.id=b.id 
set Address1=newCol1,Address2=newCol2;

如果对上述问题有任何疑问,请告诉我。

相关问题