
- 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
Select from table where value does not exist with MySQL?
For this, you can use NOT IN() −
mysql> create table DemoTable1991 ( StudentId int NOT NULL AUTO_INCREMENT PRIMARY KEY, StudentName varchar(20) ); Query OK, 0 rows affected (0.61 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable1991(StudentName) values('Chris'); Query OK, 1 row affected (0.24 sec) mysql> insert into DemoTable1991(StudentName) values('Bob'); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable1991(StudentName) values('David'); Query OK, 1 row affected (0.08 sec) mysql> insert into DemoTable1991(StudentName) values('Sam'); Query OK, 1 row affected (0.11 sec) mysql> insert into DemoTable1991(StudentName) values('Mike'); Query OK, 1 row affected (0.11 sec)
Display all records from the table using select statement −
mysql> select * from DemoTable1991;
This will produce the following output −
+-----------+-------------+ | StudentId | StudentName | +-----------+-------------+ | 1 | Chris | | 2 | Bob | | 3 | David | | 4 | Sam | | 5 | Mike | +-----------+-------------+ 5 rows in set (0.03 sec)
Here is the query to select * from table where value does not exist:
mysql> select * from DemoTable1991 where StudentName NOT IN('Bob','Sam','Mike');
This will produce the following output −
+-----------+-------------+ | StudentId | StudentName | +-----------+-------------+ | 1 | Chris | | 3 | David | +-----------+-------------+ 2 rows in set (0.04 sec)
- Related Articles
- MySQL SELECT from table A that does not exist in table B using JOINS?
- How to select from MySQL table A that does not exist in table B?
- Check if table exist without using “select from” in MySQL?
- Update only a single value from a MySQL table where select from same table ordered in descending order?
- Select MongoDB documents where a field either does not exist, is null, or is false?
- Does NOT EQUAL exist in MySQL?
- Insert array where element does not exist else update it (with multiple conditions)?
- MySQL select query to select rows from a table that are not in another table?
- What does it mean by select 1 from MySQL table?
- MySQL Select Rows where two columns do not have the same value?
- How to insert only those records that does not exist in a MySQL table?
- MySQL create user if it does not exist?
- Does SELECT TOP command exist in MySQL to select limited number of records?
- Create a table if it does not already exist and insert a record in the same query with MySQL
- Can I use two where clauses like “SELECT * FROM table WHERE condition1 and condition2” in MySQL?

Advertisements