2012-06-20 30 views
0

一些地方,我需要在MySQLSQL查詢 - 更換/移動內容

更新約2000條記錄我有一列從表「MY_TABLE」 my_content'與如下因素值

Title: some title here<br />Author: John Smith<br />Size: 2MB<br /> 

我創建3個新列(my_title,my_author和my_size),現在我需要 'my_content' 的內容是這樣

'my_title'

some title here 
分離210

'my_author'

John Smith 

'my_size'

2MB 

正如你所能想象的標題,作者和大小,始終是各行不同。

我在想什麼是查詢以下,但我不擅長SQL查詢,我不確定實際查詢的樣子。

這就是我想要做的事:

Within 'my_content' find everything that starts with "title:..." and ends with "...<br />au" and move it to 'my_title' 
Within 'my_content' find everything that starts with "thor:..." and ends with "...<br />s" and move it to 'my_author' 
Within 'my_content' find everything that starts with "ize:..." and ends with "...<br />" and move it to 'my_size' 

我只是不知道如何編寫一個查詢來做到這一點。

一旦所有的內容是在新的專欄中,我能夠找到,並刪除不需要任何更多的內容,例如「雷神:」等

回答

0

您可以使用INSTR找到的索引你的分隔符和SUBSTRING選擇你想要的部分。所以,舉例來說,筆者將

SUBSTR(my_content, 
     INSTR(my_content, "Author: ") + 8, 
     INSTR(my_content, "Size: ") - INSTR(my_content, "Author: ") - 8) 

你會需要更多的工作來修整<br/>和周圍的任何空白。

0

請嘗試以下:

SELECT SUBSTRING(SUBSTRING_INDEX(mycontent,'<br />',1),LOCATE('Title: ',mycontent)+7) as mytitle, 
SUBSTRING(SUBSTRING_INDEX(mycontent,'<br />',2),LOCATE('Author: ',mycontent)+8) as myauthor, 
SUBSTRING(SUBSTRING_INDEX(mycontent,'<br />',3),LOCATE('Size: ',mycontent)+6) as mysize 
FROM mytable; 
+0

嘿SEL,謝謝您的答覆。我只是意識到,實際上只是移動內容。我應該告訴你這是一個WordPress數據庫。很多表名是wp_postmeta,而列(mytitle,mysize和myauthor)都是與帖子相關的metabox內容。所以每篇文章都有一個mycontent的定製metabox,我需要將它分成mytitle,mysize和myauthor。 – user1467839

+0

你的意思是想用字符串的提取來更新列(mytitle,mysize和myauthor)?如果是這種情況,你可以使用'UPDATE mytable set mytitle = SUBSTRING(SUBSTRING_INDEX(mycontent,'
',1),LOCATE('Title:',mycontent)+7)' – sel