简体   繁体   中英

how to make a query update a column in a find and replace sort of fasion mysql?

I have a DB of translations, and up until now notes like Casual speech / polite speech have been put into the notes field. But now i have made another column with a list of these forms of speech. And i want to move that information thats in the notes to it. I can make this below query to set the new column based on finding the keyword "casual speech" or "polite speech" in the notes field.

UPDATE `mrhowtos_main`.`eng-jap` SET `form` = 'Casual form' WHERE `notes` LIKE '%Casual speech%';

But i want also remove that "casual speech" or "polite speech" from the notes field when i set the new form field. Notes will often contain entries like "casual speech, used by young girls". So i cant just set the field to "". or I will lose that extra information in the notes. The "casual speech" is not always at the beginning of the column, it is sometimes in the middle or the end. How can I make the query remove that part of the notes like "casual speech" as it sets the new column containing the form of speech but leave the rest of the notes?

Most likely, a SUBSTRING() should work. Something like this:

UPDATE `mrhowtos_main`.`eng-jap` 
SET `form` = 'Casual form', `notes` = SUBSTRING(`notes`, -16)
WHERE `notes` LIKE '%Casual speech%';

This assumes that any row that contains "Casual speech" actually contains "Casual speech, " . A negative value tells MySQL to start the substring that many characters from the end of the string. You can adjust as needed.

You repeat essentially the same thing for "polite speech" .

EDIT:

If the string you're looking to remove can be pretty much anywhere, REPLACE() may be a better option:

UPDATE `mrhowtos_main`.`eng-jap` 
SET `form` = 'Casual form', `notes` = REPLACE(`notes`, 'Casual speech', '')
WHERE `notes` LIKE '%Casual speech%';

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM