mysql只返回来自列的模式匹配

falq053o  于 2021-06-20  发布在  Mysql
关注(0)|答案(2)|浏览(295)

我有一个产品描述栏。我想用regexp搜索这个列,只返回regexp中的匹配项,而不是列的完整内容。
i、 我有一个描述
这颗华丽的珠子非常讲究细节
以及
这个手镯有一个磁性的盖子

SELECT description FROM products WHERE description RLIKE"'magn.*\s"

这应该会返回到壮观和磁性

SELECT description FROM products WHERE description RLIKE"'magni.*\s"

这应该会很好
当然,当前的查询返回完整的描述。任何我能做的事。MySQL5.5版本

sd2nnvve

sd2nnvve1#

如果您使用的是mysql版本8.0,那么可以使用regexp\u substr()实现此目的。此功能在早期版本中不可用。
https://dev.mysql.com/doc/refman/8.0/en/regexp.html#function_regexp-子字符串

更新

这里的另一个裂缝,它应该是5.5友好。假设提供的正则表达式位于所需匹配的开头。

SELECT 
SUBSTRING_INDEX(
  SUBSTRING(`description`, LOCATE('magni', `description`)), ' ', 1) 
FROM table1 
WHERE `description` REGEXP 'magni';

还有一个8.0版本的工作示例,供那些投票支持我之前工作的人使用

SELECT REGEXP_SUBSTR('Those magnificent men in their flying machines', '[[:space:]]magni[^ ].*[[:space:]]');
e0bqpujr

e0bqpujr2#

You can use REGEXP in mySql to solve your problem like:

SELECT description FROM products WHERE description REGEXP 'magn.*'
    The above query will give u result magnificent and magnetic

SELECT description FROM products WHERE description REGEXP 'magni.*'
    The above query will give u result magnificent

相关问题