mysql join tables code example

Example 1: inner join mysql

SELECT column_name(s)
FROM table1
INNER JOIN table2
ON table1.column_name = table2.column_name;

Example 2: mysql join query

SELECT 
    m.member_id, 
    m.name member, 
    c.committee_id, 
    c.name committee
FROM
    members m
INNER JOIN committees c 
	ON c.name = m.name;

Example 3: mysql join two tables

SELECT user_id, user_name
FROM users
UNION
SELECT organization_id, organization_name
FROM organizations

Example 4: MySQL Join

A relational database consists of multiple related tables linking together using common columns which are known as foreign key columns. Because of this, data in each table is incomplete from the business perspective.
MySQL supports the following types of joins:

Inner join
Left join
Right join
Cross join

The following shows the basic syntax of the inner join clause that joins two tables table_1 and table_2:

SELECT column_list
FROM table_1
INNER JOIN table_2 ON join_condition;


SELECT column_list
FROM table_1
INNER JOIN table_2 USING (column_name);

SELECT column_list 
FROM table_1 
LEFT JOIN table_2 USING (column_name);
Here is the syntax of the right join:

SELECT column_list 
FROM table_1 
RIGHT JOIN table_2 ON join_condition;

The following shows the basic syntax of the cross join clause:

SELECT select_list
FROM table_1
CROSS JOIN table_2;

Example 5: types of joins in mysql

Joins are used with select statement. it is used to select data from multiple table.
Types:
MySQL INNER JOIN (or simple join)
MySQL LEFT OUTER JOIN (or LEFT JOIN)
MySQL RIGHT OUTER JOIN (or RIGHT JOIN)

Inner JOIN :
The INNER JOIN is used to return all rows from multiple tables where the join condition is satisfied. It is the most common type of join.

Left Outer Join:
The LEFT OUTER JOIN returns all rows from the left hand table specified in the ON condition and only those rows from the other table where the join condition is fulfilled.

Right Outer Join:
The Right Outer Join returns all rows from the RIGHT-hand table specified in the ON condition and only those rows from the other table where he join condition is fulfilled.

Example 6: how to join result table in mysql

SELECT inv.product_id, inv_stock.stock
FROM inventories AS inv

JOIN 

(SELECT product_id, stock
FROM inventories ) AS inv_stock

ON inv.product_id = inv_stock.product_id
 ;

Tags:

Misc Example