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)