简体   繁体   English

如何用另一个数据库替换一个特定值的所有实例?

[英]How to replace all instances of a particular value in a mysql database with another?

I'm looking for a MySQL equivalent of what str_replace is for PHP. 我正在寻找与str_replace对于PHP等效的MySQL。 I want to replace all instances of one word with another, I want to run a query that will replace all "apples" with "oranges". 我想将一个单词的所有实例替换为另一个,我想运行一个查询,将所有“苹果”替换为“橙色”。

The reason why: 之所以:

UPDATE fruits SET name='oranges' WHERE name='apples'; UPDATE水果SET name ='oranges'WHERE name ='apples';

isn't going to work for my situation, is because I often times have multiple words in a table row separated by commas like: "apples, pears, pineapples". 不适用于我的情况,是因为我经常在表格行中用逗号分隔多个单词,例如:“苹果,梨,菠萝”。 In this case I want just apples to be replaced by oranges and pear and pineapples to stay in tact. 在这种情况下,我希望只用橘子代替苹果,再将梨和菠萝代替,以保持原样。

Is there any way to do this? 有什么办法吗?

Not reliably. 不可靠。 There is REPLACE() , but that will only work until you decide to add pineapples to your menu. REPLACE() ,但是只有在您决定将菠萝添加到菜单中时,该功能才起作用。

Putting your database in First Normal Form and then using UPDATE is the only reliable solution. 唯一可靠的解决方案是将数据库设置为First Normal Form ,然后使用UPDATE

You have a database design problem, as Ignacio has pointed out. 正如Ignacio指出的那样,您存在数据库设计问题。 Instead of including separate pieces of information included in a single column, that column should become a separate table with one piece of information per row. 该列应该成为一个单独的表,每行仅包含一条信息,而不是在单个列中包含单独的信息。 For instance, if that "fruits" field is in a table called "hats", you would have one table for "hats" with a column "hat_id" but no information about fruits and a second column "hat_fruits" with two columns, "hat_id" and "fruit_name". 例如,如果“ fruits”字段位于名为“ hats”的表中,则将有一个“ hats”表,其列为“ hat_id”, 但没有关于水果的信息 ,第二列为“ hat_fruits”,其列为两列,“ hat_id”和“ fruit_name”。 In your example, the given hat would have three rows in "hat_fruits", one for each fruit. 在您的示例中,给定的帽子在“ hat_fruits”中具有三行,每个水果一行。

Once you implement this design (if you have control of the database design) you can go back to use the simple UPDATE command you originally had. 一旦实现了此设计(如果您可以控制数据库设计),则可以返回原来使用的简单UPDATE命令。 In addition, you will be able to index by fruit type, search more easily, use less disk space, validate fruit names, and not have any arbitrary limit on the number of fruits that fit into the database 此外,您将能够按水果类型建立索引,更轻松地搜索,使用更少的磁盘空间,验证水果名称,并且对适合数据库的水果数量没有任何限制

That said, if you absolutely cannot fix the database structure, you might try something like this: 就是说,如果您绝对无法修复数据库结构,则可以尝试如下操作:

 REPLACE(REPLACE(CONCAT(',', fruits, ','), ', ', ','), ',apples,', ',oranges,')

This monstrosity first converts the fruits field to begin and end with commas, then removes any spaces before commas. 这种怪兽首先将Fru​​its字段转换为以逗号开头和结尾,然后删除逗号之前的所有空格。 This should give you a string in which fruit names are unambiguously delimited by commas. 这应该为您提供一个字符串,其中的水果名称明确地由逗号分隔。 Finally, it replaces the ,apples, (note the delimiters) with ,oranges,. 最后,它用,oranges替换了,apples(注意分隔符)。

After that, of course, you ought to strip off the beginning and ending commas and put back the spaces after the commas (that's left as an exercise for the reader). 当然,在那之后,您应该删除开头和结尾的逗号,并在逗号之后放回空格(留给读者练习)。

Update : Okay, I couldn't resist looking it up: 更新 :好的,我忍不住要查找它:

 REPLACE(TRIM(',' FROM REPLACE(REPLACE(CONCAT(',', fruits, ','), ', ', ','), ',apples,', ',oranges,')), ',', ' ,')

Note that this isn't tested and I'm not a MySQL expert anyway — I don't know if MySQL has function nesting issues or anything like that. 请注意,这未经测试,而且我也不是MySQL专家-我不知道MySQL是否存在函数嵌套问题或类似问题。

PS: Don't tell anyone I was the one who showed you this! PS:不要告诉任何人我是向您展示此内容的人!

I think you want to use REPLACE() : 我认为您想使用REPLACE()

REPLACE(str,from_str,to_str) 替换(str,from_str,to_str)

Returns the string str with all occurrences of the string from_str replaced by the string to_str. 返回字符串str,其中所有出现的字符串from_str都替换为字符串to_str。 REPLACE() performs a case-sensitive match when searching for from_str. 搜索from_str时,REPLACE()执行区分大小写的匹配。

对于“水果”表中的所有行,下面将在“名称”列中将所有出现的“苹果”替换为“橙色”。

UPDATE fruits SET Name=REPLACE(Name,'apples','oranges')

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

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