Several commonly used Mysql connections join

1. First, prepare two lists

Sector Table:

  

Employees table:

  

The following table we have these two different connection operation

1. the connector

  Role: Query two tables of common parts

   语句:Select <select_list> from tableA A Inner join tableB B on A.Key = B.Key

   Example: SELECT * from employee e INNER JOIN department d on e.dep_id = d.id;
   The results showed: with this method to find, as we have not found id data 8

     

2. Left connection

    Role: the contents of the left side of the table all found the right table only identify records satisfy the condition

  语句:Select <select_list> from tableA A Left Join tableB B on A.Key = B.Key  

  示例:SELECT * from employee e LEFT JOIN department d on e.dep_id = d.id;

  The results show:

    

3. The right connection

   Role: the contents of the table to the right of all identified, left the table only identify records satisfy the condition

  语句:Select <select_list> from tableA A Left Join tableB B on A.Key = B.Key

  Example: SELECT * from employee e RIGHT JOIN department d on e.dep_id = d.id;
  The results showed that:

    

4. Query left table unique data

  Role: A unique data query of

  语句:Select <select_list> from tableA A Left Join tableB B on A.Key = B.Key where B.key IS NULL   

  Example: SELECT * from employee e LEFT JOIN department d on e.dep_id = d.id WHERE d.id IS NULL;
  The results showed that:

    

5. Query exclusive right table data

  Role: A unique data query B,

  语句:Select <select_list> from tableA A Right Join tableB B on A.Key = B.Key where A.key IS NULL   

  示例:SELECT * from employee e RIGHT JOIN department d on e.dep_id = d.id WHERE e.id IS NULL;

  The results show:

    

6. fully connected

   Role: query all the information two tables

  语句:Select <select_list> from tableA A Full Outter Join tableB B on A.Key = B.Key

  Note: Mysql default does not support this wording Oracle support can be used to connect the left and right connections to work together as a full connection

  Example:  

    SELECT * from employee e LEFT JOIN department d on e.dep_id = d.id

    UNION

    SELECT * from employee e RIGHT JOIN department d on e.dep_id = d.id

  The results show:

    

 7. queries about their unique data table

  Role: A and B each query unique data

  语句:Select <select_list> from tableA A Full Outter Join tableB B on A.Key = B.Key where A.key = null or B.key=null    

  示例:
    SELECT * from employee e LEFT JOIN department d on e.dep_id = d.id WHERE d.id is NULL

    UNION

    SELECT * from employee e RIGHT JOIN department d on e.dep_id = d.id WHERE e.dep_id is NULL

  The results show:

    

 

Guess you like

Origin www.cnblogs.com/pcliu/p/11099761.html