How do I exclude a specific record in MySQL?


You can exclude a specific record in SQL using not equal to operator(!=). Let us first create a table−

mysql> create table DemoTable
   (
   Id int NOT NULL AUTO_INCREMENT PRIMARY KEY,
   ClientName varchar(20),
   ClientCountryName varchar(10)
   );
Query OK, 0 rows affected (0.64 sec)

Insert records in the table using insert command −

mysql> insert into DemoTable(ClientName,ClientCountryName) values('John','US');
Query OK, 1 row affected (0.20 sec)
mysql> insert into DemoTable(ClientName,ClientCountryName) values('David','AUS');
Query OK, 1 row affected (0.23 sec)
mysql> insert into DemoTable(ClientName,ClientCountryName) values('Mike','UK');
Query OK, 1 row affected (0.14 sec)

Display all records from the table using select statement −

mysql> select *from DemoTable;

This will produce the following output −

+----+------------+-------------------+
| Id | ClientName | ClientCountryName |
+----+------------+-------------------+
| 1  | John       | US                |
| 2  | David      | AUS               |
| 3  | Mike       | UK                |
+----+------------+-------------------+
3 rows in set (0.00 sec)

Following is the query to exclude a specific record in MySQL i.e. we are excluding records with ClientName David' or ClientCountryName ='AUS'−

mysql> select *from DemoTable where ClientName!='David' or ClientCountryName!='AUS';

This will produce the following output−

+----+------------+-------------------+
| Id | ClientName | ClientCountryName |
+----+------------+-------------------+
| 1  | John       | US                |
| 3  | Mike       | UK                |
+----+------------+-------------------+
2 rows in set (0.00 sec)

Updated on: 30-Jul-2019

351 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements