使用 LIKE 和 OR 进行文本搜索的 MySQL 查询以获取记录
我们首先创建一个表 −
mysql> create table DemoTable ( Subject text ); Query OK, 0 rows affected (0.86 sec)
使用 insert 命令在表中插入一些记录 −
mysql> insert into DemoTable values('Introduction to MySQL'); Query OK, 1 row affected (0.31 sec) mysql> insert into DemoTable values('Deep Dive using Java'); Query OK, 1 row affected (0.17 sec) mysql> insert into DemoTable values('C in Depth'); Query OK, 1 row affected (0.17 sec) mysql> insert into DemoTable values('Introduction to C++'); Query OK, 1 row affected (0.48 sec)
使用 select 语句从表中显示所有记录 −
mysql> select *from DemoTable;
这将产生以下输出 −
+-----------------------+ | Subject | +-----------------------+ | Introduction to MySQL | | Deep Dive using Java | | C in Depth | | Introduction to C++ | +-----------------------+ 4 rows in set (0.00 sec)
以下是使用 LIKE 和 OR 进行文本搜索的查询 −
mysql> select *from DemoTable where Subject LIKE '%MySQL%' OR Subject LIKE '%using%';
这将产生以下输出 −
+-----------------------+ | Subject | +-----------------------+ | Introduction to MySQL | | Deep Dive using Java | +-----------------------+ 2 rows in set (0.00 sec)
广告