简体   繁体   English

如何使查询更新查找和替换fasion mysql中的列?

[英]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. SUBSTRING()最有可能工作。 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, " . 假设任何包含"Casual speech"实际上都包含"Casual speech, " A negative value tells MySQL to start the substring that many characters from the end of the string. 负值告诉MySQL从字符串的末尾开始许多字符。 You can adjust as needed. 您可以根据需要进行调整。

You repeat essentially the same thing for "polite speech" . 对于"polite speech"您基本上重复了同样的事情。

EDIT: 编辑:

If the string you're looking to remove can be pretty much anywhere, REPLACE() may be a better option: 如果您要删除的字符串几乎在任何地方,那么REPLACE()可能是一个更好的选择:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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