Mysql
 sql >> Base de Dados >  >> RDS >> Mysql

SQL:pesquisa/substitui, mas apenas na primeira vez que um valor aparece no registro


Isso deve realmente ser o que você quer no MySQL:
UPDATE wp_post
SET post_content = CONCAT(REPLACE(LEFT(post_content, INSTR(post_content, 'A')), 'A', 'B'), SUBSTRING(post_content, INSTR(post_content, 'A') + 1));

É um pouco mais complicado do que minha resposta anterior - você precisa encontrar a primeira instância do 'A' (usando a função INSTR), então use LEFT em combinação com REPLACE para substituir apenas essa instância, do que usar SUBSTRING e INSTR para encontrar o mesmo 'A' você está substituindo e CONCAT com a string anterior.

Veja meu teste abaixo:
SET @string = 'this is A string with A replace and An Answer';
SELECT @string as actual_string
, CONCAT(REPLACE(LEFT(@string, INSTR(@string, 'A')), 'A', 'B'), SUBSTRING(@string, INSTR(@string, 'A') + 1)) as new_string;

Produz:
actual_string                                  new_string
---------------------------------------------  ---------------------------------------------
this is A string with A replace and An Answer  this is B string with A replace and An Answer