MYSQL: How to search and replace data in one table a table with column from another table using a REGEXP

好久不见. 提交于 2019-12-11 19:39:22

问题


Here's the basic idea of what I need to do. I have two tables like the following:

Table A               Table B
|id|name       |     |id|sentence                                       |
|1 |hello      |     |1 |I like to say hello                            |
|2 |world      |     |2 |The world is large                             |
|3 |hello world|     |3 |lorem hello ipsum world lorem ipsum hello world|

I need to cycle through the rows in Table A to look for each name in Table B. Then I want to use a REGEXP in Table B to replace the word if it is found.

For example if hello is found in Table B it will replace the word hello in Table B with #~Hello~# So the fist row in table B will turn into I like to say #~Hello~#

Thanks to Gordon Linoff for a great question. So I am updating the question.

Not only can rows in Table B contain multiple words from Table A, but Table A can also have rows that contain multiple word combinations. See row 3 in Table A for reference.

In this case the longest word combination should get replaced and the shorter combination/words should be skipped. A proper working of the code would produce a Table B row 3 like the following:

lorem #~hello~# ipsum #~world~# lorem ipsum #~hello world~#

Any ideas?

来源:https://stackoverflow.com/questions/33056127/mysql-how-to-search-and-replace-data-in-one-table-a-table-with-column-from-anot

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!