- 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
MySQL SELECT from table A that does not exist in table B using JOINS?
To SELECT from table A that does not exist in table B, you can use left join. Following is the syntax −
select yourTableNameA.* from yourTableNameA left join yourTableNameB on yourTableNameA.yourColumnName = yourTableNameB.yourColumnName where yourTableNameB.yourColumnName IS NULL;
Let us first create a table. Following is the query −
<table_A>
mysql> create table table_A -> ( -> Value int -> ); Query OK, 0 rows affected (1.10 sec)
Following is the query to insert records in the table using insert command −
mysql> insert into table_A values(10); Query OK, 1 row affected (0.32 sec) mysql> insert into table_A values(15); Query OK, 1 row affected (0.10 sec) mysql> insert into table_A values(35); Query OK, 1 row affected (0.21 sec) mysql> insert into table_A values(45); Query OK, 1 row affected (0.13 sec)
Following is the query to display all records from the table using select statement −
mysql> select *from table_A;
This will produce the following output −
+-------+ | Value | +-------+ | 10 | | 15 | | 35 | | 45 | +-------+ 4 rows in set (0.00 sec)
Let us create another table. Following is the query −
<table_B>
mysql> create table table_B -> ( -> Value int -> ); Query OK, 0 rows affected (0.51 sec)
Following is the query to insert some records in the table using insert command −
mysql> insert into table_B values(10); Query OK, 1 row affected (0.18 sec) mysql> insert into table_B values(20); Query OK, 1 row affected (0.12 sec) mysql> insert into table_B values(35); Query OK, 1 row affected (0.23 sec) mysql> insert into table_B values(60); Query OK, 1 row affected (0.12 sec)
Following is the query to display all records from the table using a select statement −
mysql> select *from table_B;
This will produce the following output −
+-------+ | Value | +-------+ | 10 | | 20 | | 35 | | 60 | +-------+ 4 rows in set (0.00 sec)
Following is the query to select from table A that does not exist in table B with LEFT JOIN −
mysql> select table_A.* -> from table_A left join table_B on table_A.Value = table_B.Value -> where table_B.Value IS NULL;
This will produce the following output −
+-------+ | Value | +-------+ | 15 | | 45 | +-------+ 2 rows in set (0.04 sec)
- Related Articles
- How to select from MySQL table A that does not exist in table B?
- Select from table where value does not exist with MySQL?
- Check if table exist without using “select from” in MySQL?
- MySQL select query to select rows from a table that are not in another table?
- How to insert only those records that does not exist in a MySQL table?
- Select rows from a MySQL table and display using IN()
- What does it mean by select 1 from MySQL table?
- Insert values in a table by MySQL SELECT from another table in MySQL?
- How to check if a table exists in MySQL and create if it does not already exist?
- Select random row that exists in a MySQL table?
- In MySQL, how can we maintain data-driven table relationship using joins?
- Create a table if it does not already exist and insert a record in the same query with MySQL
- Does NOT EQUAL exist in MySQL?
- PHP and MYSQL database connection and table creation only once if it does not already exist?
- How to select all the data from a table using MySQL in Python?
