Database-mysql Data Connection

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.