使用 LIKE 在 MySQL 中很常见。我们这样使用它:WHERE field LIKE '%substring%'
。我们有一个子字符串,字段有完整的字符串。但我需要的是相反的东西。我在字段中有子字符串。所以,我想要包含我的字符串的子字符串的那一行。假设表是:
----+-------------------
id | keyword
----+-------------------
1 | admission
----+-------------------
2 | head of the dept
----+-------------------
我有一个来自用户的字符串:告诉我关于入场信息
。我需要这样一个返回 admission
的 MySQL 查询,因为这是用户字符串的子字符串。像这样的东西:
SELECT keyword FROM table WHERE (keyword is a substring of 'Tell me about admission info')
提前致谢。
最佳答案
您正在寻找 LIKE运算符(operator)
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, '%'))
SQL Fiddle DEMO
关于mysql - 选择包含子字符串的 MySQL 字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18809976/