mysql调用其他表数据,使用另一个表中的数据在MySQL中查找和替换字符串

I have two MySQL tables, and I want to find and replace text strings in one using data in another.

Table texts:

+---------------------+

| messages |

+---------------------+

| 'thx guys' |

| 'i think u r great' |

| 'thx again' |

| ' u rock' |

+---------------------+

Table dictionary:

+--------------+---------------+

| bad_spelling | good_spelling |

+--------------+---------------+

| 'thx' | 'thanks' |

| ' u ' | ' you ' |

| ' r ' | ' are ' |

+--------------+---------------+

I want SQL to go through and look at every row in messages and replace every instance of bad_spelling with good_spelling, and to do this for all the pairs of bad_spelling and good_spelling.

The closest I have gotten is this:

update texts, dictionary

set texts.message = replace(texts.message,

dictionary.bad_spelling,

dictionary.good_spelling)

But this only changes "thx" to "thanks" (in two rows) and does not go on to replace " u " with " you" or " r " with " are ."

Any ideas how to make it use all the rows in dictionary in the replace statement?

PS forgot to mention that this is a small example and in the real thing I will have a lot of find/replace pairs, which may get added to over time.

解决方案

I've never used MySql, so this is just a theory based on my other database work. When reading the other answers, trying to use REPLACE(), I thought I could post this and get someone with MySql syntax experience a few ideas to make a set base solution.

here is some SQL Server code to that does most of the work for you:

DECLARE @Source table (Texts varchar(50))

INSERT @Source VALUES ('thx guys')

INSERT @Source VALUES ('i think u r great')

INSERT @Source VALUES ('thx again')

INSERT @Source VALUES ('u rock')

DECLARE @Dictionary table (bad_spelling varchar(50), good_spelling varchar(50))

INSERT @Dictionary VALUES ('thx', 'thanks')

INSERT @Dictionary VALUES ('u', 'you')

INSERT @Dictionary VALUES ('r', 'are')

SELECT

t.Texts,COALESCE(d.good_spelling,c.ListValue) AS WordToUse

FROM @Source t

CROSS APPLY dbo.FN_ListToTable(' ',t.Texts) c

LEFT OUTER JOIN @Dictionary d ON c.ListValue=d.bad_spelling

OUTPUT:

Texts WordToUse

------------------ ---------

thx guys thanks

thx guys guys

i think u r great i

i think u r great think

i think u r great you

i think u r great are

i think u r great great

thx again thanks

thx again again

u rock you

u rock rock

(11 row(s) affected)

It would be better to use a "real" PK than the actual "Texts" in the query above, but the OP doesn't list many columns in that table, so I use "Texts".

Using SQL Server you need to use a some funky XML syntax to join the rows back together (so I won't show that code, as it doesn't matter), but using MySql's GROUP_CONCAT() you should be able to concatenate the word rows back together into phrase rows.

the code for the (SQL Server) split function and how it works can be found here: SQL Server: Split operation

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值