从MySQL中的一列中删除子字符串

时间:2018-07-01 11:02:48

标签: php mysql substring

我想从表的列中删除部分子字符串

例如,我的结构如下:

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),'')";

但是这行不通!

1 个答案:

答案 0 :(得分:1)

尝试使用此查询来分隔地址:

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;

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