检查一个字符串是否在一个 mysql 行中包含一个值(子串)的 mysql 查询?
既然我们需要匹配同一行中的字符串,那就使用 LIKE 运算符。我们首先创建一个表 -
mysql> create table DemoTable ( FirstName varchar(100), FullName varchar(100) ); Query OK, 0 rows affected (0.53 sec)
使用 insert 命令在表中插入一些记录 -
mysql> insert into DemoTable values('John','John Smith');
Query OK, 1 row affected (0.08 sec)
mysql> insert into DemoTable values('David','John Miller');
Query OK, 1 row affected (0.14 sec)
mysql> insert into DemoTable values('Bob','Sam Miller');
Query OK, 1 row affected (0.18 sec)
mysql> insert into DemoTable values('Chris','Chris Brown');
Query OK, 1 row affected (0.10 sec)使用 select 语句显示表中所有记录 -
mysql> select *from DemoTable;
这会产生以下输出 -
+-----------+-------------+ | FirstName | FullName | +-----------+-------------+ | John | John Smith | | David | John Miller | | Bob | Sam Miller | | Chris | Chris Brown | +-----------+-------------+ 4 rows in set (0.00 sec)
以下查询用于检查一个字符串是否在一个行中包含一个值 -
mysql> select FirstName,FullName from DemoTable
where FullName LIKE concat('%', FirstName, '%');这会产生以下输出 -
+-----------+-------------+ | FirstName | FullName | +-----------+-------------+ | John | John Smith | | Chris | Chris Brown | +-----------+-------------+ 2 rows in set (0.00 sec)
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
安卓
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP