包含子字符串的SELECT MySQL字段 [英] SELECT MySQL field that contains a substring

查看:179
本文介绍了包含子字符串的SELECT MySQL字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在MySQL中,使用LIKE很常见.我们这样使用它:WHERE field LIKE '%substring%'.我们有一个子字符串,而字段有完整的字符串.但是我需要的是相反的东西.我在字段中有子字符串.因此,我希望该行包含我的字符串的子字符串.假设表是:

Using LIKE is very common in MySQL. We use it like this: WHERE field LIKE '%substring%'. Where we have a substring and field has full string. But what I need is something opposite. I have substrings in field. So, I want that row which contains a substring of my string. Suppose the table is:

----+-------------------
 id | keyword
----+-------------------
  1 | admission
----+-------------------
  2 | head of the dept
----+-------------------

,我有一个来自用户的字符串:Tell me about admission info.我需要一个返回admission的MySQL查询,因为这是用户字符串的子字符串.像这样:

and I have a string from user: Tell me about admission info. I need such a MySQL query that returns admission as this is a substring of user string. Something like:

SELECT keyword FROM table WHERE (keyword is a substring of 'Tell me about admission info')

提前谢谢.

推荐答案

您正在寻找

使用SQL简单正则表达式比较的模式匹配. 返回1(TRUE)或0(FALSE).如果expr或pat为NULL,则 结果为NULL.

Pattern matching using SQL simple regular expression comparison. Returns 1 (TRUE) or 0 (FALSE). If either expr or pat is NULL, the result is NULL.

类似

SELECT  keyword 
FROM    table 
WHERE   ('Tell me about admission info' LIKE CONCAT('%', keyword, '%'))

这篇关于包含子字符串的SELECT MySQL字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆