MySQL 如何检查某一列中的任何字符串是否包含特定字符串
为此,需要使用CONCAT()运算符和LIKE运算符。让我们首先创建一个表 –
mysql> create table DemoTable
(
Name varchar(40)
);
Query OK, 0 rows affected (0.56 sec)
使用insert命令在表中插入一些记录 –
mysql> insert into DemoTable values('John');
Query OK, 1 row affected (0.33 sec)
mysql> insert into DemoTable values('Adam');
Query OK, 1 row affected (0.11 sec)
mysql> insert into DemoTable values('Bob');
Query OK, 1 row affected (0.22 sec)
mysql> insert into DemoTable values('Johnson');
Query OK, 1 row affected (0.13 sec)
mysql> insert into DemoTable values('David');
Query OK, 1 row affected (0.12 sec)
使用select语句显示表中的所有记录 –
mysql> select *from DemoTable;
这将产生以下输出 –
+---------+
| Name |
+---------+
| John |
| Adam |
| Bob |
| Johnson |
| David |
+---------+
5 rows in set (0.00 sec)
以下是检查某一列中的任何字符串是否包含特定字符串的查询 –
mysql> select *from DemoTable where Name like "%Joh%" OR "Joh" LIKE CONCAT("%", Name, "%");
这将产生以下输出 –
+---------+
| Name |
+---------+
| John |
| Johnson |
+---------+
2 rows in set (0.00 sec)
阅读更多:MySQL 教程