2

I need to update some links in my database that look like this:

<a href="http://www.domain.com.au/PDF/Rapidvite/Action_K_Salts.pdf">http://www.domain.com.au/PDF/Rapidvite/Accelerator_Oil.pdf</a>
<a href="http://www.domain.com.au/PDF/Rapidvite/Action_K_Salts.pdf">http://www.domain.com.au/PDF/Rapidvite/Accelerator_Oil.pdf</a>

And I need to replace the text with 'View PDF' (without the quotes) whilst keeping the href so that they look like this

<a href="http://www.domain.com.au/PDF/Rapidvite/Action_K_Salts.pdf">View PDF</a>
<a href="http://www.domain.com.au/PDF/Rapidvite/Action_K_Salts.pdf">View PDF</a>

Here is my wild stab in the dark - very new to MySQL

UPDATE `ayfryvrxb2_postmeta` SET meta_value = replace(meta_value, '.pdf">%%</a>', '.pdf">View PDF</a>"');
4

2 回答 2

0

我使用 - 它不一定是最好的方式,但当我从概念转向生活或其他方式时,它适用于我的网站 - 这个:

UPDATE `wp_posts` SET `post_content` = REPLACE(`post_content`, '<OLD URL'>, <'NEW URL'>;
UPDATE `wp_postmeta` SET `meta_value` = REPLACE(`meta_value`, '<OLD URL'>, <'NEW URL'>;

如果我留下了语法错误,我深表歉意,但希望你能明白我所做的事情的要点。如果您可能在自定义字段中有链接,第二个命令很有用,我经常使用它。

于 2013-10-10T12:54:26.050 回答
0

试试这个对我有用的sql命令

SELECT  REPLACE('<a href="http://www.domain.com.au/PDF/Rapidvite/Action_K_Salts.pdf">http://www.domain.com.au/PDF/Rapidvite/Accelerator_Oil.pdf</a>',


      ( SUBSTR('<a href="http://www.domain.com.au/PDF/Rapidvite/Action_K_Salts.pdf">http://www.domain.com.au/PDF/Rapidvite/Accelerator_Oil.pdf</a>',
      (LOCATE('.pdf">', '<a href="http://www.domain.com.au/PDF/Rapidvite/Action_K_Salts.pdf">http://www.domain.com.au/PDF/Rapidvite/Accelerator_Oil.pdf</a>') + 6),
      LOCATE('.pdf</a>', '<a href="http://www.domain.com.au/PDF/Rapidvite/Action_K_Salts.pdf">http://www.domain.com.au/PDF/Rapidvite/Accelerator_Oil.pdf</a>')) )
      ,'View PDF</a>'
      )

FROM your_table h

但是你必须<a href="http://www.domain.com.au/PDF/Rapidvite/Action_K_Salts.pdf">http://www.domain.com.au/PDF/Rapidvite/Accelerator_Oil.pdf</a>用你的表格列替换我的硬编码字符串。例如,如果您使用这些链接名称进行列my_table_links

那么最终的 sql 查询将如下所示

SELECT 
REPLACE(my_table_links,


      ( SUBSTR(my_table_links,
      (LOCATE('.pdf">', my_table_links) + 6),
      LOCATE('.pdf</a>', my_table_links)) )
      ,'View PDF</a>'
       )

FROM `my_table` h

最后在更新情况下,它将是

UPDATE `ayfryvrxb2_postmeta` SET 

meta_value =  REPLACE(meta_value,


          ( SUBSTR(meta_value,
          (LOCATE('.pdf">', meta_value) + 6),
          LOCATE('.pdf</a>', meta_value)) )
          ,'View PDF</a>'
           )
where 1;
于 2013-10-10T11:27:53.753 回答