MySQL:返回Y字之前和之后的X字
我已经尝试了几天了,但无法让它完全按照需要工作,并且没有任何搜索对我有帮助。
在我的数据库中,我有一些带有链接的文章,如下所示:
one two three four five six seven eight nine ten eleven twelve thirteen fourteen
<a href="google.com>Google!</a> one two three four five six seven eight nine ten
我想要做的是找到文章中的链接并报告前面最多 5 个单词和后面最多 5 个单词(例如)
所以我的结果是类似于:
ten eleven twelve thirteen fourteen
<a href="google.com>Google!</a> one two three four five
任何人都可以帮我查询来完成此任务吗?
I've been trying this for a few days now and have been unable to get it working exactly as needed and no amount of searching has helped me.
In my database I have articles with links in them like so:
one two three four five six seven eight nine ten eleven twelve thirteen fourteen
<a href="google.com>Google!</a> one two three four five six seven eight nine ten
What I want to be able to do is find the link within the article and report up to 5 words before and up to 5 words after (for example)
So my result would be something like:
ten eleven twelve thirteen fourteen
<a href="google.com>Google!</a> one two three four five
Can anyone help me with a query to accomplish this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
请参阅http://dev.mysql.com/doc/refman/ 5.5/en/string-functions.html 文档。
这适用于字符串中的第一个链接。顺便说一句,我会在我的演示代码中执行此操作(在您的例子中是 PHP),但令人惊讶的是您可以在 SQL 中执行此操作。请务必检查您的输入(WHERE quote LIKE '%
See http://dev.mysql.com/doc/refman/5.5/en/string-functions.html for the docs.
This works for the first link in the string. BTW, I would do this in my presentation code (PHP in yr case), but it's surprising what you can do in the SQL. Make sure to check your input (WHERE quote LIKE '% <a href="%</a>%') because input with no link will probably not give you what you want.
默认情况下你不能这样做。正则表达式仅返回布尔值,但如果您使用外部正则表达式插件,像这个,它也许有可能。
You can't do that by default. Regexp only gives back boolean values, but if you use an external regexp plugin, like this one, it might be possible.