在 MySQL 中,有没有办法选择部分匹配的值?

mysqlmysqli database

要部分匹配,请使用 LIKE 运算符。让我们首先创建一个表 −

mysql> create table DemoTable806(
   StudentId int NOT NULL AUTO_INCREMENT PRIMARY KEY,
   StudentName varchar(100),
   StudentSubject varchar(100)
);
Query OK, 0 rows affected (0.57 sec)

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

mysql> insert into DemoTable806(StudentName,StudentSubject) values('Chris','Java in Depth With Data Structure');
Query OK, 1 row affected (0.15 sec)
mysql> insert into DemoTable806(StudentName,StudentSubject) values('Robert','Introduction to MySQL');
Query OK, 1 row affected (0.14 sec)
mysql> insert into DemoTable806(StudentName,StudentSubject) values('Bob','C++ in Depth With Data Structure And Algorithm');
Query OK, 1 row affected (0.14 sec)
mysql> insert into DemoTable806(StudentName,StudentSubject) values('Adam','Introduction to MongoDB');
Query OK, 1 row affected (0.11 sec)

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

mysql> select *from DemoTable806;

这将产生以下输出 −

+-----------+-------------+------------------------------------------------+
| StudentId | StudentName | StudentSubject                                 |
+-----------+-------------+------------------------------------------------+
|         1 | Chris       | Java in Depth With Data Structure              |
|         2 | Robert      | Introduction to MySQL                          |
|         3 | Bob         | C++ in Depth With Data Structure And Algorithm |
|         4 | Adam        | Introduction to MongoDB                        |
+-----------+-------------+------------------------------------------------+
4 rows in set (0.00 sec)

以下是选择部分匹配的值的查询 −

mysql> select *from DemoTable806 where StudentSubject LIKE '%Depth%';

这将产生以下输出 −

+-----------+-------------+------------------------------------------------+
| StudentId | StudentName | StudentSubject                                 | 
+-----------+-------------+------------------------------------------------+
|         1 | Chris       | Java in Depth With Data Structure              |
|         3 | Bob         | C++ in Depth With Data Structure And Algorithm |
+-----------+-------------+------------------------------------------------+
2 rows in set (0.00 sec)

相关文章