mysql/mariadb learning record - query (JOIN)

// The data table used in this article 
//id_p is the primary key in the persons table//id_o is the primary key
in the orders table, and id_p is the foreign key. Refer to id_p in the persons table
mysql> select * from persons; select * from orders; +--- ---+------------+------------+----------------+------ ----+ | id_p | lastname | firstname | address | city | +------+----------+-----------+----------------+----------+ | 1 | Adams | John | Oxford Street | London | | 2 | Bush | George | Fifth Avenue | New York | | 3 | Carter | Thomas | Changan Street | Beijing | +------+----------+-----------+----------------+----------+ 3 rows in set (0.05 sec) +------+---------+------+ | id_o | orderNo | id_p | +------+---------+------+ | 1 | 77895 | 3 | | 2 | 44678 | 3 | | 3 | 22456 | 1 | | 4 | 24562 | 1 | | 5 | 34764 | 65 | +------+---------+------+ 5 rows in set (0.05 sec)

 

1. Inner join [inner] join:

Inner joins are also called joins, or natural joins:

//inner可删
mysql> select lastname,firstname,orderNo from persons inner join orders on persons.id_p=orders.id_p order by lastname;
+----------+-----------+---------+
| lastname | firstname | orderNo |
+----------+-----------+---------+
| Adams    | John      | 24562   |
| Adams    | John      | 22456   |
| carter | Thomas | 77895    |
| carter | Thomas | 44678    |
+----------+-----------+---------+

// Use where to achieve the same effect 
mysql> select lastname,firstname,orderNo from persons,orders where persons.id_p= orders.id_p;
 +----------+--------- --+---------+
| lastname | firstname | orderNo |
+----------+-----------+---------+
| carter | Thomas | 77895    |
| carter | Thomas | 44678    |
| Adams    | John      | 22456   |
| Adams    | John      | 24562   |
+----------+-----------+---------+

2. Left join left [outer] join:

First look at the left join statement and query results

//outer可删去
mysql> select lastname,firstname,orderNo from persons left join orders on persons.id_p=orders.id_p order by lastname;
+----------+-----------+---------+
| lastname | firstname | orderNo |
+----------+-----------+---------+
| Adams    | John      | 24562   |
| Adams    | John      | 22456   |
| Bush     | George    | NULL    |
| carter | Thomas | 77895    |
| carter | Thomas | 44678    |
+----------+-----------+---------+

Through this result, we can find that the lastname is Bush and there is no orderNo, while the lastname and firstname attributes are from the persons table, and the orderNo attribute is from the orders table

3. Right join right [outer] join:

//outer可删去
mysql> select lastname,firstname,orderNo from persons right outer join orders on persons.id_p=orders.id_p order by lastname;
+----------+-----------+---------+
| lastname | firstname | orderNo |
+----------+-----------+---------+
| NULL | NULL | 34764    |
| Adams    | John      | 22456   |
| Adams    | John      | 24562   |
| carter | Thomas | 77895    |
| carter | Thomas | 44678    |
+----------+-----------+---------+

4. Full join (but not supported by mysql/mariadb)

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325313209&siteId=291194637