一个 MySQL 查询可用来从不同列值搜索多个单词


为此,可以使用 WHERE 子句与多个 LIKE。让我们先创建一个表 −

mysql> create table DemoTable1536
   -> (
   -> Sentence text
   -> );
Query OK, 0 rows affected (0.51 sec)

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

mysql> insert into DemoTable1536 values('I like MySQL database.');
Query OK, 1 row affected (0.14 sec)
mysql> insert into DemoTable1536 values('Java is an Object Oriented Programming Language');
Query OK, 1 row affected (0.30 sec)
mysql> insert into DemoTable1536 values('I only like data structure');
Query OK, 1 row affected (0.21 sec)
mysql> insert into DemoTable1536 values('MongoDB is NoSQL database');
Query OK, 1 row affected (0.46 sec)

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

mysql> select * from DemoTable1536;

这将生成以下输出 −

+--------------------------------------------------+
| Sentence                                         |
+--------------------------------------------------+
| I like MySQL database.                           |
| Java is an Object Oriented Programming Language  |
| I only like data structure                       |
| MongoDB is NoSQL database                        |
+--------------------------------------------------+
4 rows in set (0.00 sec)

以下是搜索多个单词的查询 −

mysql> select * from DemoTable1536
   -> where ( Sentence like '%like%') or (Sentence like '%database%') or (Sentence like '%data%');

这将生成以下输出 −

+----------------------------+
| Sentence                   |
+----------------------------+
| I like MySQL database.     |
| I only like data structure |
| MongoDB is NoSQL database  |
+----------------------------+
3 rows in set (0.00 sec)

更新日期:12-Dec-2019

1K+ 浏览

开启您的事业

通过完成课程获得认证

开始
广告