One: Use of Mysql connection
In the previous chapters, we have learned that it is relatively straightforward to read data in a table, but it is often necessary to read data from multiple data tables in a real application.
In this section we will show you how to use MySQL JOIN to query data in two or more tables.
You can federate multiple table queries using Mysql JOIN in SELECT, UPDATE, and DELETE statements.
JOIN is broadly divided into three categories as follows:
- INNER Join (inner JOIN, or equivalent connection): Gets a record of the field matching relationship in two tables.
- left join: gets all the records of the left table, even if the right table does not have a matching record.
- Right join: the opposite of the left join is used to get all the records of the right table, even if there are no matching records for the table.
- Full join two tables and set, MySQL does not directly support, but through the Unicon implementation
Two: Inner connection
Intersection of two tables
MariaDB [test2]> SELECT * fromA;+------+| Name |+------+| 1 | | 2 | | 3 | | 4 |+------+4 rowsinchSet (0.00sec) MariaDB [Test2]> select * fromb;+------+| Name |+------+| 3 | | 4 | | 5 | | 6 |+------+4 rowsinchSet (0.00sec) MariaDB [Test2]>SELECT * from a inner join B on a.name=b.name; +------+------+| name | Name |+------+------+| 3 | 3 | | 4 | 4 |+------+------+2 rowsinchSet (0.00sec) MariaDB [Test2]> select * fromA, b where a.name=B.name;+------+------+| name | Name |+------+------+| 3 | 3 | | 4 | 4 |+------+------+2 rowsinchSet (0.00 sec)
Three: Left JOIN
MySQL left join differs from join. The MySQL left JOIN reads all the data from the data table on the right, even if there is no corresponding data on the table.
from a LEFT join B on A.name=b.name; +------+------+| name | Name |+------+------+| 3 | 3 | | 4 | 4 | | 1 | NULL | | in Set (0.00 sec)
Four: Right JOIN
MySQL right JOIN reads all the data from the data table on the left side, even if there is no data on the side table.
from a right join B on a.name=b.name; +------+------+| name | Name |+------+------+| 3 | 3 | | 4 | 4 | | NULL | 5 | | NULL | in Set (0.00 sec)
V: Full Join
Set of two tables
from From a RIGHT join B on a.name =b.name; +------+------+| name | Name |+------+------+| 3 | 3 | | 4 | 4 | | 1 | NULL | | 2 | NULL | | NULL | 5 | | NULL | in Set (0.00 sec)
Database-mysql Data Connection