MySQL 查询以匹配列值中的任意两个字符串

mysqlmysqli database更新于 2025/10/30 13:32:17

为此,您可以使用 LIKE 运算符和 OR 条件。

首先我们创建一个表 −

mysql> create table DemoTable762 (Title text);
Query OK, 0 rows affected (0.54 sec)

使用 insert 命令在表中插入一些记录 −

mysql> insert into DemoTable762 values('Introduction to Java');
Query OK, 1 row affected (0.19 sec)
mysql> insert into DemoTable762 values('MySQL is a RDBMS');
Query OK, 1 row affected (0.19 sec)
mysql> insert into DemoTable762 values('Data Structure and Algorithm in Java');
Query OK, 1 row affected (0.21 sec)
mysql> insert into DemoTable762 values('Data Structure and Algorithm in C and C++');
Query OK, 1 row affected (0.18 sec)

使用 select 语句显示表中的所有记录 −

mysql> select *from DemoTable762 ;

这将产生以下输出 -

+-------------------------------------------+
| Title                                     |
+-------------------------------------------+
| Introduction to Java                      |
| MySQL is a RDBMS                          |
| Data Structure and Algorithm in Java      |
| Data Structure and Algorithm in C and C++ |
+-------------------------------------------+
4 rows in set (0.00 sec)

以下是匹配两个字符串"MySQL"或"Algorithm"中的任意一个的查询 −

mysql> select *from DemoTable762 where Title LIKE '%MySQL%' or Title LIKE '%Algorithm%';

这将产生以下输出 -

+-------------------------------------------+
| Title                                     |
+-------------------------------------------+
| MySQL is a RDBMS                          |
| Data Structure and Algorithm in Java      |
| Data Structure and Algorithm in C and C++ |
+-------------------------------------------+
3 rows in set (0.00 sec)

相关文章


有用资源