使用 MySQL 根据必要条件过滤表中的数据以消除特定记录
mysqlmysqli database
为此,您可以使用 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)