
- Learn MySQL
- MySQL - Home
- MySQL - Introduction
- MySQL - Installation
- MySQL - Administration
- MySQL - PHP Syntax
- MySQL - Connection
- MySQL - Create Database
- MySQL - Drop Database
- MySQL - Select Database
- MySQL - Data Types
- MySQL - Create Tables
- MySQL - Drop Tables
- MySQL - Insert Query
- MySQL - Select Query
- MySQL - Where Clause
- MySQL - Update Query
- MySQL - Delete Query
- MySQL - Like Clause
- MySQL - Sorting Results
- MySQL - Using Join
- MySQL - NULL Values
- MySQL - Regexps
- MySQL - Transactions
- MySQL - Alter Command
- MySQL - Indexes
- MySQL - Temporary Tables
- MySQL - Clone Tables
- MySQL - Database Info
- MySQL - Using Sequences
- MySQL - Handling Duplicates
- MySQL - SQL Injection
- MySQL - Database Export
- MySQL - Database Import
How to select all the records except a row with certain id from a MySQL table?
To avoid displaying a certain id from a table, you need to use the <> operator, which is the NOT EQUAL operator. Let us first create a table −
mysql> create table DemoTable7 ( StudentId int NOT NULL AUTO_INCREMENT PRIMARY KEY, StudentName varchar(40) ); Query OK, 0 rows affected (0.53 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable7(StudentName) values('Chris'); Query OK, 1 row affected (0.25 sec) mysql> insert into DemoTable7(StudentName) values('Robert'); Query OK, 1 row affected (0.11 sec) mysql> insert into DemoTable7(StudentName) values('Mike'); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable7(StudentName) values('Sam'); Query OK, 1 row affected (0.61 sec)
Display all records from the table using select statement −
mysql> select *from DemoTable7;
This will produce the following output −
+-----------+-------------+ | StudentId | StudentName | +-----------+-------------+ | 1 | Chris | | 2 | Robert | | 3 | Mike | | 4 | Sam | +-----------+-------------+ 4 rows in set (0.00 sec)
Following is the query to select all the records except a certain ID using the NOT EQUAL operator −
mysql> select *from DemoTable7 where StudentId <> 2;
This will produce the following output −
+-----------+-------------+ | StudentId | StudentName | +-----------+-------------+ | 1 | Chris | | 3 | Mike | | 4 | Sam | +-----------+-------------+ 3 rows in set (0.06 sec)
- Related Articles
- How to select all rows from a table except the last one in MySQL?
- MySQL query to select records beginning from a specific id
- MySql how to display the records with latest ID in a table?
- MySQL query to select all the records only from a specific column of a table with multiple columns
- How to select records beginning with certain numbers in MySQL?
- Delete all the records from a MySQL table?
- Delete all records from a table in MySQL?
- Select all rows except from today in MySQL?
- How can I select the row with the highest ID in MySQL?
- Select a fixed number of random records from a MySQL table?
- How can we fetch all the records from a particular MySQL table?
- How to convert all the records in a MySQL table from uppercase to lowercase?
- Fetch maximum ID value from the first table and insert to all the IDs in another table with MySQL INSERT INTO select?
- How to select all the data from a table using MySQL in Python?
- Select records from a table on the basis of keywords in MySQL

Advertisements