- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Exclude some ID records from a list and display rest in MySQL
To exclude records, use MySQL NOT IN(). Let us first create a table −
mysql> create table DemoTable ( Id int ); Query OK, 0 rows affected (0.64 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable values(1); Query OK, 1 row affected (0.16 sec) mysql> insert into DemoTable values(2); Query OK, 1 row affected (0.11 sec) mysql> insert into DemoTable values(3); Query OK, 1 row affected (0.22 sec) mysql> insert into DemoTable values(4); Query OK, 1 row affected (0.41 sec) mysql> insert into DemoTable values(5); Query OK, 1 row affected (0.09 sec) mysql> insert into DemoTable values(6); 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 −
+------+ | Id | +------+ | 1 | | 2 | | 3 | | 4 | | 5 | | 6 | +------+ 6 rows in set (0.00 sec)
Following is the query to exclude records −
mysql> select *from DemoTable where Id NOT IN(1,4,6,3);
This will produce the following output −
+------+ | Id | +------+ | 2 | | 5 | +------+ 2 rows in set (0.00 sec)
- Related Articles
- MySQL query to order records but fix a specific name and display rest of the values (only some) random
- Display records from the current date till rest of the same month in MySQL?
- MySql how to display the records with latest ID in a table?
- Fetch similar ID records from two tables in MySQL
- MySQL query to select records beginning from a specific id
- Display table records from a stored procedure in MySQL
- Order by a single field and display rest of the records in the same order with MySQL
- Find “greatest” between two columns and display with some records already null in MySql
- MySQL query to exclude some of the values from the table
- Display selected records from a MySQL table with IN() operator
- MySQL search and replace record from a list of records
- MySQL query to display records ordered by DESC while skipping some?
- Find and display duplicate records in MySQL?
- From a list of IDs with empty and non-empty values, retrieve specific ID records in JavaScript
- Display distinct dates in MySQL from a column with date records

Advertisements