【发布时间】:2010-03-24 17:13:58
【问题描述】:
我有两个 MySQL 表,我想使用另一个表中的数据查找和替换其中的文本字符串。
表texts:
+---------------------+
| messages |
+---------------------+
| 'thx guys' |
| 'i think u r great' |
| 'thx again' |
| ' u rock' |
+---------------------+
表dictionary:
+--------------+---------------+
| bad_spelling | good_spelling |
+--------------+---------------+
| 'thx' | 'thanks' |
| ' u ' | ' you ' |
| ' r ' | ' are ' |
+--------------+---------------+
我希望 SQL 遍历并查看消息中的每一行,并将 bad_spelling 的每个实例替换为 good_spelling,并对所有 bad_spelling 和 good_spelling 对执行此操作。
我得到的最接近的是:
update texts, dictionary
set texts.message = replace(texts.message,
dictionary.bad_spelling,
dictionary.good_spelling)
但这只会将“thx”更改为“thanks”(在两行中),并且不会继续将“u”替换为“you”或“r”替换为“are”。
任何想法如何使它在替换语句中使用字典中的所有行?
PS 忘了提到这是一个小例子,实际上我会有很多查找/替换对,随着时间的推移可能会添加。
【问题讨论】: