用于匹配列值中任意两个字符串的 MySQL 查询
基于这个,你可以将 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)
广告