这是“能做到吗”的问题之一。我有一位同事联系我,要求从数据库文本字符串中删除网址(除了以 http 开头之外,所有网址都是唯一的)。我的第一反应是使用替换功能,但这可能会变得使用和维护起来非常麻烦。
因此,我在论坛上询问他们对如何最好地完成这项任务的想法。
This is a test http://t.co/aBc689XYz -> new result=This is a test
Have a nice http://t.co/vZ754PlkuI day -> new result=Have a nice day
If the URL
部分在文本中仅存在一次,以下内容应该有效。
MySQL解决方案:
select concat( @pss:=substring_index( txt, 'http://', 1 ),
substring( @ss:=substring_index( txt, 'http://', -1 ),
if( (@l:=locate( ' ', @ss )) > 0, @l+1, 0 ) )
) as txt
from (
select 'This is a test http://t.co/aBc689XYz' as txt
union all
select 'Have a nice http://t.co/vZ754PlkuI day'
union all
select 'This worked http://sqlfiddle.com/#!2/d41d8 perfectly on sql fiddle'
) records
;
Results:
+-------------------------------------+
| txt |
+-------------------------------------+
| This is a test |
| Have a nice day |
| This worked perfectly on sql fiddle |
+-------------------------------------+
Demo @ MySQL 5.5.32 小提琴 http://sqlfiddle.com/#!2/d41d8/36031
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)