
- 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
MySQL join two tables?
Let us first create two tables and join them with foreign key constraint. The query to create the first table is as follows −
mysql> create table ParentTable -> ( -> UniqueId int NOT NULL AUTO_INCREMENT PRIMARY KEY, -> EmployeeName varchar(10) -> ); Query OK, 0 rows affected (0.56 sec)
Insert some records in the first table using insert command. The query is as follows −
mysql> insert into ParentTable(EmployeeName) values('John'); Query OK, 1 row affected (0.15 sec) mysql> insert into ParentTable(EmployeeName) values('Carol'); Query OK, 1 row affected (0.32 sec) mysql> insert into ParentTable(EmployeeName) values('Sam'); Query OK, 1 row affected (0.18 sec) mysql> insert into ParentTable(EmployeeName) values('Bob'); Query OK, 1 row affected (0.19 sec)
Now you can display all records from the table using a select statement. The query is as follows −
mysql> select *from ParentTable;
The following is the output −
+----------+--------------+ | UniqueId | EmployeeName | +----------+--------------+ | 1 | John | | 2 | Carol | | 3 | Sam | | 4 | Bob | +----------+--------------+ 4 rows in set (0.00 sec)
The query to create the second table with foreign key constraints is as follows −
mysql> create table ChildTable -> ( -> UniqueId int NOT NULL PRIMARY KEY, -> EmployeeAddress varchar(100), -> CONSTRAINT fk_uniqueId FOREIGN KEY(UniqueId) references ParentTable(UniqueId) -> ); Query OK, 0 rows affected (0.54 sec)
Now insert some records in the second table using insert command. The query is as follows −
mysql> insert into ChildTable values(1,'15 West Shady Lane Starkville, MS 39759'); Query OK, 1 row affected (0.19 sec) mysql> insert into ChildTable values(2,'72 West Rock Creek St. Oxford, MS 38655'); Query OK, 1 row affected (0.18 sec) mysql> insert into ChildTable(UniqueId) values(3); Query OK, 1 row affected (0.41 sec) mysql> insert into ChildTable values(4,'119 North Sierra St. Marysville, OH 43040'); Query OK, 1 row affected (0.16 sec)
Display all records from the table using a select statement. The query is as follows −
mysql> select *from ChildTable;
The following is the output −
+----------+-------------------------------------------+ | UniqueId | EmployeeAddress | +----------+-------------------------------------------+ | 1 | 15 West Shady Lane Starkville, MS 39759 | | 2 | 72 West Rock Creek St. Oxford, MS 38655 | | 3 | NULL | | 4 | 119 North Sierra St. Marysville, OH 43040 | +----------+-------------------------------------------+ 4 rows in set (0.00 sec)
Let us now join the tables using a left join. The query is as follows −
mysql> select ParentTable.UniqueId,ParentTable.EmployeeName,ChildTable.EmployeeAddress from ParentTable left join -> ChildTable on ParentTable.UniqueId=ChildTable.UniqueId;
The following is the output −
+----------+--------------+-------------------------------------------+ | UniqueId | EmployeeName | EmployeeAddress | +----------+--------------+-------------------------------------------+ | 1 | John | 15 West Shady Lane Starkville, MS 39759 | | 2 | Carol | 72 West Rock Creek St. Oxford, MS 38655 | | 3 | Sam | NULL | | 4 | Bob | 119 North Sierra St. Marysville, OH 43040 | +----------+--------------+-------------------------------------------+ 4 rows in set (0.00 sec)
- Related Articles
- Perform MySQL LEFT JOIN on two tables?
- Compare two tables and return missing ids using MySQL LEFT OUTER JOIN
- How can you perform inner join on two tables using MySQL in Python?
- How can you perform left join on two tables using MySQL in Python?
- How can you perform right join on two tables using MySQL in Python?
- How can you perform full join on two tables using MySQL in Python?
- How can you perform self join on two tables using MySQL in Python?
- How to write PHP script by using MySQL JOINS inside it to join two MySQL tables?
- How to join tables and fetch values from a MySQL database?
- How to join tables in SAP system
- How can I merge two MySQL tables?
- Merge two tables with union in MySQL?
- Concatenate two tables in MySQL with a condition?
- Get MAX() on column in two MySQL tables?
- How can we compare data in two MySQL tables?

Advertisements