MySQL: 在评论字符串中搜索关键字

MySQL: Search keyword in a comment string

我在 Whosebug 上浏览了几个与我的问题相似的类似主题,但我还没有找到任何可以帮助我的东西。我有这个 SQL 查询:

SELECT * FROM twitter_result 
WHERE LOWER(TweetComment) LIKE LOWER('%lebron james%') 
AND LOWER(TweetComment) LIKE LOWER('%NBA%')

我想搜索同时包含单词 "LeBron James" 和 "NBA" 的 TweetComment。但这两个词需要单独存在。喜欢它不应该 return 包含#LeBron James 和#NBA(或 NBATalk)的推文

例如,它应该 return 像这样的推文

LeBron James Donates Million To Send 1,100 Kids To College, Becomes 6th Most Charitable Athlete NBA In World

勒布朗·詹姆斯和 NBA 是独立的(没有 # 个字符)。我有 LOWER 忽略区分大小写。任何帮助是极大的赞赏。谢谢

抱歉我忘了添加,我只是在 PHPMyAdmin

中使用 SQL

虽然有使用正则表达式的解决方案,但如果不知道您使用的数据库是很难提出的。

相反,您可以在执行 like:

之前删除不需要的标签
WHERE REPLACE(LOWER(TweetComment), '#lebron james', '') LIKE LOWER('%lebron james%') AND
      REAPLCE(LOWER(TweetComment), '#nba', '') LIKE LOWER('%NBA%')

如果您打算使用regexp

select * from twitter_result
where --ignore tweets that contain #lebron james and #nba
      TweetComment not regexp '.*#lebron james.*|.*#nba.*' 
      --select only those tweets that contain lebron james AND nba
and   TweetComment regexp '[[:<:]]lebron james[[:>:]]'
and   TweetComment regexp '[[:<:]]nba[[:>:]]'

所有正在搜索的模式都必须明确说明,因为 MySQL 默认情况下不支持环视。

以上匹配默认不区分大小写。如果搜索需要区分大小写,请使用 regexp binary。根据需要添加更多搜索词。

Sample fiddle