2010-12-23 161 views

回答

3

更新:

update MyTable 
set MyColumn = concat('[videolog]', replace(SUBSTRING(SUBSTRING_INDEX(MyColumn, '/', -1), 2), '.flv', '[/videolog]')) 
where MyColumn like 'http://play.videolog.tv/videos/%' 
+0

@virendar:我讀得太快,並沒有意識到* xx *值是數字。看到我上面的更新版本。 – RedFilter 2010-12-24 02:31:03

0

這需要一些串魔術做到這一點完全在MySQL。我相信這應該工作:

UPDATE posts_table 
SET posts_table.post_text = REPLACE(posts_table.post_text, @full_url := MID(posts_table.post_text, @url_start := LOCATE('http://play.videolog.tv/videos/', posts_table.post_text), LOCATE('.flv', posts_table.post_text, @url_start) + 4 - @url_start), 
CONCAT('[videolog]', MID(@full_url, LENGTH(@full_url) - LOCATE('/', REVERSE(@full_url)) + 2, 6), '[/videolog]')) 
WHERE LOCATE('http://play.videolog.tv/videos/', posts_table.post_text) 

你可以用這個查詢測試:

SELECT REPLACE(posts_table.post_text, @full_url := MID(posts_table.post_text, @url_start := LOCATE('http://play.videolog.tv/videos/', posts_table.post_text), LOCATE('.flv', posts_table.post_text, @url_start) + 4 - @url_start), 
CONCAT('[videolog]', MID(@full_url, LENGTH(@full_url) - LOCATE('/', REVERSE(@full_url)) + 2, 6), '[/videolog]')) AS `result` 
FROM posts_table 
WHERE LOCATE('http://play.videolog.tv/videos/', posts_table.post_text) 

只需更換posts_table和POST_TEXT用正確的表/列名。

該查詢應該替換該URL的所有實例,即使它位於一堆文本的中間。