- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- 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)
To Continue Learning Please Login
Login with Google