过滤数据表格特定条件以消除特定记录(使用 MySQL)


为此,可以使用 NOT LIKE 运算符。我们首先创建一个表格 -

mysql> create table DemoTable
   -> (
   -> StudentId int NOT NULL AUTO_INCREMENT PRIMARY KEY,
   -> StudentName varchar(20),
   -> StudentAdmissionYear varchar(20)
   -> );
Query OK, 0 rows affected (1.22 sec)

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

mysql> insert into DemoTable(StudentName,StudentAdmissionYear) values('Chris','2017');
Query OK, 1 row affected (0.14 sec)
mysql> insert into DemoTable(StudentName,StudentAdmissionYear) values('David','2015');
Query OK, 1 row affected (0.14 sec)
mysql> insert into DemoTable(StudentName,StudentAdmissionYear) values('Bob','2019');
Query OK, 1 row affected (0.18 sec)
mysql> insert into DemoTable(StudentName,StudentAdmissionYear) values('Carol','2015');
Query OK, 1 row affected (0.11 sec)
mysql> insert into DemoTable(StudentName,StudentAdmissionYear) values('Sam','2018');
Query OK, 1 row affected (0.12 sec)

使用 select 语句从表格中显示所有记录 -

mysql> select *from DemoTable;

这将产生以下输出 -

+-----------+-------------+----------------------+
| StudentId | StudentName | StudentAdmissionYear |
+-----------+-------------+----------------------+
|         1 | Chris       | 2017                 |
|         2 | David       | 2015                 |
|         3 | Bob         | 2019                 |
|         4 | Carol       | 2015                 |
|         5 | Sam         | 2018                 |
+-----------+-------------+----------------------+
5 rows in set (0.00 sec)

以下是过滤数据表格特定条件以消除特定记录的查询 -

mysql> select *from DemoTable
   -> where StudentAdmissionYear not like '%2015%';

这将产生以下输出。在这里,我们消除了 StudentAdmissionYear 2015 的记录 -

+-----------+-------------+----------------------+
| StudentId | StudentName | StudentAdmissionYear |
+-----------+-------------+----------------------+
|         1 | Chris       | 2017                 |
|         3 | Bob         | 2019                 |
|         5 | Sam         | 2018                 |
+-----------+-------------+----------------------+
3 rows in set (0.04 sec)

更新于: 2019 年 12 月 12 日

59 次浏览

开启您的 职业生涯

完成课程并获得认证

开始
广告
© . All rights reserved.