MySQL替换字符串中的变量

时间:2015-06-22 12:30:30

标签: mysql css replace

我收到了一些旧的产品数据,在产品描述中,他们为不同的产品添加了自己的风格。它是在大约8年前制造的。 现在我得到了他们自己的所有数据,我需要删除像witdh这样的样式。 宽度变化很大,所以我可以宽度:150px到宽度:300px等。

<table style="width: 500px;" border="0" cellspacing="1" cellpadding="1">

我需要将其删除,以便在移动设备或平板电脑上缩小时能够快速响应。

我的替换应该如何?我可以使用通配符来解决这个问题,还是只需要接受字符串的其余部分并删除它?

REPLACE(`product_desc`, 'style="width:%"', '')

1 个答案:

答案 0 :(得分:0)

如果模式相同且表格只有width: Npx;,那么您可以执行以下操作 -

select 
 replace(
   '<table style="width: 500px;" border="0" cellspacing="1" cellpadding="1">',
   concat(
      'width: ',
      trim( 
        substring_index(
         substring_index(
           '<table style="width: 500px;" border="0" cellspacing="1" cellpadding="1">','width:',-1
         ),
        '"',1
        )
       )
     ),
 '') as a ;

结果是

+-------------------------------------------------------------+
| a                                                           |
+-------------------------------------------------------------+
| <table style="" border="0" cellspacing="1" cellpadding="1"> |
+-------------------------------------------------------------+
1 row in set (0.00 sec)

您可以将此转换为更新语句

update table_name 
set product_desc = 
replace(
   product_desc,
   concat(
      'width: ',
      trim( 
        substring_index(
         substring_index(
           product_desc,'width:',-1
         ),
        '"',1
        )
       )
     ),
 '')
相关问题