Filtering data in a table for a required condition to eliminate a specific record with MySQL


For this, you can use NOT LIKE operator. Let us first create a table −

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 some records in the table using insert command −

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)

Display all records from the table using select statement −

mysql> select *from DemoTable;

This will produce the following output −

+-----------+-------------+----------------------+
| 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)

Following is the query to filter data in a table for a required condition to eliminate a specific record −

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

This will produce the following output. Here, we have eliminated records with StudentAdmissionYear 2015 −

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

Updated on: 12-Dec-2019

38 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements