【问题标题】:Replace bulk values in a field - MySql替换字段中的批量值 - MySql
【发布时间】:2015-05-09 18:43:03
【问题描述】:

我在“wp_postmeta”表中的“meta_value”字段中保存了一些 html 代码,如下所示:<img src="...." height="X" width="Y" > 我想用“100%”替换所有字段中的 X 和 Y,但这在 MySql 中可行吗? X 和 Y 在每条记录中是不同的值,例如 X=200,Y=350 - 第一条记录,X=325,Y=200 在第二条记录中等等。

我看到了Mysql search and replace some text in a field 的问题,但我可以使用类似的解决方案来更新 height="X" 和 width="Y" 的批量值吗?

谢谢!

【问题讨论】:

  • 提供一些实际的例子,不清楚数据是如何存储在表中的。

标签: mysql


【解决方案1】:

mysql默认没有MariaDB这样的功能:

REGEXP_REPLACE(subject, pattern, replace)

您可以创建一个函数,如本博客所示:https://techras.wordpress.com/2011/06/02/regex-replace-for-mysql/

【讨论】:

    【解决方案2】:

    这很复杂,让我们从示例开始,我们如何使用 mysql 来做到这一点。

    mysql> create table test (post text);
    Query OK, 0 rows affected (0.12 sec)
    
    mysql> insert into test values ('<img src="...." height="100" width="200">'),('<img src="...." height="300" width="400" >');
    Query OK, 2 rows affected (0.07 sec)
    Records: 2  Duplicates: 0  Warnings: 0
    
    mysql> select * from test ;
    +--------------------------------------------+
    | post                                       |
    +--------------------------------------------+
    | <img src="...." height="100" width="200">  |
    | <img src="...." height="300" width="400" > |
    +--------------------------------------------+
    

    现在让我们使用一些函数来进行替换

    select 
    replace
    (
      post,
      concat(
        'height="',
        substring_index(
         substring_index(
          post,'height="',-1
         ),
         '"',1
        ),
        '"'
       ),
       concat(
        concat(
         'height="',substring_index(
           substring_index(
            post,'height="',-1
           ),
          '"',1
         ),
        '%"'
       )
      )
    ) as height from test;
    
    +---------------------------------------------+
    | height                                      |
    +---------------------------------------------+
    | <img src="...." height="100%" width="200">  |
    | <img src="...." height="300%" width="400" > |
    +---------------------------------------------+
    2 rows in set (0.00 sec)
    

    同样的宽度可以替换为

    select 
    replace
    (
      post,
      concat(
        'width="',
        substring_index(
         substring_index(
          post,'width="',-1
         ),
         '"',1
        ),
        '"'
       ),
       concat(
        concat(
         'width="',substring_index(
           substring_index(
            post,'width="',-1
           ),
          '"',1
         ),
        '%"'
       )
      )
    ) as width from test;
    
    +---------------------------------------------+
    | width                                       |
    +---------------------------------------------+
    | <img src="...." height="100" width="200%">  |
    | <img src="...." height="300" width="400%" > |
    +---------------------------------------------+
    2 rows in set (0.01 sec)
    

    最后在更新命令中使用它们

    update test set
    text =  
    replace
    (
      post,
      concat(
        'height="',
        substring_index(
         substring_index(
          post,'height="',-1
         ),
         '"',1
        ),
        '"'
       ),
       concat(
        concat(
         'height="',substring_index(
           substring_index(
            post,'height="',-1
           ),
          '"',1
         ),
        '%"'
       )
      )
    ),
    text = 
    replace
    (
      post,
      concat(
        'width="',
        substring_index(
         substring_index(
          post,'width="',-1
         ),
         '"',1
        ),
        '"'
       ),
       concat(
        concat(
         'width="',substring_index(
           substring_index(
            post,'width="',-1
           ),
          '"',1
         ),
        '%"'
       )
      )
    ) ;
    
    
    mysql> select * from test ;
    +----------------------------------------------+
    | post                                         |
    +----------------------------------------------+
    | <img src="...." height="100%" width="200%">  |
    | <img src="...." height="300%" width="400%" > |
    +----------------------------------------------+
    2 rows in set (0.00 sec)
    

    【讨论】:

      猜你喜欢
      • 2015-02-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-04-08
      • 1970-01-01
      • 2011-03-09
      • 1970-01-01
      • 2014-07-29
      相关资源
      最近更新 更多