Ceci est une ancienne révision du document !
REPLACE / UPDATE (remplacer / rechercher)
Pour remplacer des chaînes de caractères dans des bases de données MySQL
http://sql.sh/fonctions/replace
REPLACE(str,from_str,to_str)
Returns the string str with all occurrences of the string from_str replaced by the string to_str. REPLACE() performs a case-sensitive match when searching for from_str.
mysql> SELECT REPLACE('www.mysql.com', 'w', 'Ww');
-> 'WwWwWw.mysql.com'
UPDATE table
SET nom_colonne = REPLACE(nom_colonne, 'ancien texte', 'texte de remplacement')
ex:
UPDATE dc_post
SET post_excerpt = replace(post_excerpt,"cms/dotclear","blog");
ex. avec php
$replace = mysql_query(" replace into recet (id, prov, titre, temps, ingr, pers, typ, prep, date) VALUES ('$id', '$prov', '$titre', '$temps', '$ingr', '$pers', '$typ', '$prep', '$date')");
UPDATE
update sr_data set email='@bcu.unil.ch' where email='FR' # update signets set category='Geneve_et le_monde' where category='Genève_et le_monde'
Rechercher / Remplacer un string de caractères
descriptif du problème
dans le champ titre de la table recettes, j'aimerais remplacer toutes les occurences de “calmar” par “calamar”, exemple:
Ojing-eo sundae (boudin de seiche ou calmar)
→
Ojing-eo sundae (boudin de seiche ou calamar)
solution
UPDATE recettes SET titre = REPLACE(titre, 'calmar', 'calamar') WHERE titre LIKE '%calmar%';
si vous avez des case-sensitive:
UPDATE recettes SET titre = REPLACE(REPLACE(REPLACE(titre, 'calmar', 'calamar'), 'Calmar', 'Calamar'), 'CALMAR', 'CALAMAR') WHERE titre LIKE '%calmar%' OR titre LIKE '%Calmar%' OR titre LIKE '%CALMAR%';
Pour changer un sous-string précis dans 1 champs
dans le champs glossaires, remplacer toutes les extensions .jpg en .webp:
UPDATE glossaires SET image = REPLACE(image, '.jpg', '.webp') WHERE image LIKE '%.jpg';
Pour changer un string précis dans 1/n champs
UPDATE xoops_bb_posts_text
SET post_text=(
REPLACE
(post_text,'morphix.sourceforge.net','www.morphix.org')
);
marche pas (infra)
UPDATE recet SET ingr =
REPLACE ( ingr, 'calmar', 'calamar' )
WHERE ingr LIKE '%calmar%'
CA CA MARCHE:
update `tableName`
set `Field`=replace('string','old','new')