English 中文(简体)
利用另一个表格的数据,在MySQL中选取和替换插座
原标题:Find and replace string in MySQL using data from another table
  • 时间:2010-03-24 17:13:58
  •  标签:
  • sql
  • mysql

我有两张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      |
+--------------+---------------+

我想走到电文中,看一看每一行,用好卡片来取代每一件坏事,并为所有坏人做这件事。

我最接近的是:

update texts, dictionary
set texts.message = replace(texts.message,
                            dictionary.bad_spelling,
                            dictionary.good_spelling)

但是,这只是将“thx”改为“thanks”(两行),而不再将“u”改为“You”或“r”改为“the ”。

任何想法如何在替代声明中使用词句?


PS必须提到,这是一个小的例子,在我会发现/替代奶制品的实际情况中,这种情况可能会随着时间的推移而增加。

最佳回答

我从未使用过MySql,因此,这只是基于我其他数据库工作的理论。 在阅读其他答复时,我试图利用REPLACE(REPLACE),我认为我可以作此发言,让一位有MySql syntax的人了解一些想法,以便找到一套基本的解决办法。

这里是一些服务器代码,大部分工作是:

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)

采用“real”PK比上述查询中的实际“Texts”要好,但是,OP没有在表中列出许多栏,因此我使用“Texts”。

利用QL 服务器需要使用某种幻灯XML辛塔,以便与浏览站一起(因此,我只得 t地显示,照此刻,密码是用MySql小组——CONCAT(CONCAT)——你应该能够把字句重新加起来。

http://stackoverflow.com/questions/2507330/sqlserver-split-operation/2507408#2507408" 服务器:Split 行动

问题回答

这并非完全一样,因为即使更换时间为x倍(X是字典中的行数),但只保留了一次更新(最后一次)。

交易没有勾销中间结果,因此可以把它们视为下批替代物的输入值。

As (AFAIK) 我的SQL不支持你不得不采用程序做法的复述询问。

你们需要多次执行问询。 由于这是你通常偶尔使用的<代码>clean-up型号”的操作,因此建议你在做一些更新之前进行以下询问。 我不知道如何使用<条码>MySql,但在<条码>SQL上,它将检查更新的浏览数(这是<条码>>>UPDATE查询执行的结果),并在没有更新浏览量之前再操作<条码>UPD<>。

update  texts, 
        dictionary
set     texts.message = replace(texts.message, dictionary.bad_spelling, dictionary.good_spelling)
where   texts.message <> replace(texts.message, dictionary.bad_spelling, dictionary.good_spelling)

您必须多次在案文上替换:

Update ...
Set texts.message = Replace(
                        Replace(
                            Replace( texts.message,  thx  ,  thanks   )
                            ,   u  ,   you  )
                        ,   r  ,   are  )

http://www.ohchr.org。 鉴于你说,你有许多替代物,你需要用多份最新资料的 cur子这样做。 比如(我只字不做过测试,这样就知道):

Create Temporary Table ReplaceValues 
    (
    BeforeText varchar(100) not null
    , AfterText varchar(100) not null
    )

Insert ReplaceValues(BeforeText, AfterText) Values( thx  ,  thanks  )
Insert ReplaceValues(BeforeText, AfterText) Values(  u  ,   you  )
Insert ReplaceValues(BeforeText, AfterText) Values(  r  ,   are  )

DECLARE done int DEFAULT(0)
DECLARE BeforeValue varchar(100);
DECLARE AfterValue varchar(100);
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = 1;

DECLARE ReplaceList CURSOR FOR Select BeforeText, AfterText From ReplaceValues;

OPEN ReplaceList;   

REPEAT
    If NOT done THEN
        FETCH ReplaceList INTO BeforeValue, AfterValue;

        Update texts
        Set texts.message = REPLACE(texts.message, BeforeValue, AfterValue);
    END IF  
UNTIL done END REPEAT;
CLOSE ReplaceList;

您可以把所有这些内容总结为一种程序,以便你能在晚些时候再次提出。





相关问题
SQL SubQuery getting particular column

I noticed that there were some threads with similar questions, and I did look through them but did not really get a convincing answer. Here s my question: The subquery below returns a Table with 3 ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

php return a specific row from query

Is it possible in php to return a specific row of data from a mysql query? None of the fetch statements that I ve found return a 2 dimensional array to access specific rows. I want to be able to ...

Character Encodings in PHP and MySQL

Our website was developed with a meta tag set to... <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> This works fine for M-dashes and special quotes, etc. However, I ...

Pagination Strategies for Complex (slow) Datasets

What are some of the strategies being used for pagination of data sets that involve complex queries? count(*) takes ~1.5 sec so we don t want to hit the DB for every page view. Currently there are ~...

Averaging a total in mySQL

My table looks like person_id | car_id | miles ------------------------------ 1 | 1 | 100 1 | 2 | 200 2 | 3 | 1000 2 | 4 | 500 I need to ...

热门标签