mysql - 选择包含子字符串的 MySQL 字段

标签 mysql sql string

使用 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/

相关文章:

sql - 使用简单更新的计数器列的原子增量

c# - 修改 C# 中的 "G"字符串格式化程序以在数字包含至少两个零 d.p 时使用指数

MySQL Workbench 数据导出(架构丢失!!)

PHP 和 mySQL - 将单个 HTML 字段文本解析/分离为 2 个变量

sql - 如何将时间戳插入postgreSQL数据库

asp.net - 公共(public) ASP.NET 应用程序安全注意事项

r - 字符串分解

java - 使用 myString.split 时遇到问题 ("\n");

MySQL查询 - 获取未购买单个产品ID所有部分的客户

mysql - 如何在 Livecode 应用程序中将 UTF-8 数据保存在 MySQL 数据库中