Mysql connection usage instance, mysql usage instance

Source: Internet
Author: User

Mysql connection usage instance, mysql usage instance

This article describes the usage of mysql connections between left and right. Share it with you for your reference. The details are as follows:

Use an example to parse the left, right, and Inner Connections of mysql.
Copy codeThe Code is as follows: create table user_id (id decimal (18 ));
Create table user_profile (id decimal (18), name varchar (255 ));
Insert into user_id values (1 );
Insert into user_id values (2 );
Insert into user_id values (3 );
Insert into user_id values (4 );
Insert into user_id values (5 );
Insert into user_id values (6 );
Insert into user_id values (1 );

Insert into user_profile values (1, "aa ");
Insert into user_profile values (2, "bb ");
Insert into user_profile values (3, "cc ");
Insert into user_profile values (4, "dd ");
Insert into user_profile values (5, "ee ");
Insert into user_profile values (5, "EE ");
Insert into user_profile values (8, 'zz ');

1. Left join:
Copy codeThe Code is as follows: mysql> select a. id, ifnull (B. name, 'n'/A') name from user_id A left join user_profile B on a. id = B. id;
Mysql> select a. id, ifnull (B. name, 'n'/A') name from user_id A left join user_profile B on a. id = B. id;
+ ------ +
| Id | name |
+ ------ +
| 1 | aa |
| 2 | bb |
| 3 | cc |
| 4 | dd |
| 5 | ee |
| 5 | EE |
| 6 | N/A |
| 1 | aa |
+ ------ +
8 rows in set (0.00 sec)

User_id is left, so it is the left join. In this case, all records in user_id are listed. There are three situations:

1. If the id of each record in user_id also exists in user_profile and there is only one record, a new record will be generated in the returned result. For example, 1, 2, 3, and 4.
2. If the id of each record in user_id also exists in user_profile and there are N records, N new records will be generated in the returned results. As shown in figure 5 above.
3. If the id of each record in user_id does not exist in user_profile, a new record is generated in the returned result, and all the records on the right are NULL. As shown in figure 6 above.

Records that do not comply with the preceding three rules will not be listed.

For example, to query data that does not exist in a related table, use id Association to find records that do not exist in user_profile in the user_id table:
Copy codeThe Code is as follows: select count (*) from user_id left join user_profile on user_id.id = user_profile.id where user_profile.id is null;

2. Right join

User_profile is the right link. In this case, all records of user_profile are listed. There are three situations:

1. If the id of each record in user_profile also exists in user_id and there is only one record, a new record will be generated in the returned result. As shown in the above 2, 3, 4, 5.
2. If the id of each record in user_profile also exists in user_id and there are N records, N new records will be generated in the returned results. As shown in figure 1 above.
3. If the id of each record in user_profile does not exist in user_id, a new record is generated in the returned result, and all the records on the left are NULL. As shown in figure 8 above.

Records that do not comply with the preceding three rules will not be listed.

Iii. Internal Connection

There is no NULL field in the data records connected in MySQL. It can be simply considered that the result of the inner link is the result obtained after the record with the NULL field is removed from the result of the Left or Right join. In addition, MySQL does not support full join.
Copy codeThe Code is as follows: mysql> select * from user_id a inner join user_profile B on a. id = B. id;
+ ------ +
| Id | name |
+ ------ +
| 1 | 1 | aa |
| 1 | 1 | aa |
| 2 | 2 | bb |
| 3 | 3 | cc |
| 4 | 4 | dd |
| 5 | 5 | ee |
| 5 | 5 | EE |
+ ------ +
7 rows in set (0.00 sec)
Mysql> select * from user_id a, user_profile B where a. id = B. id;
+ ------ +
| Id | name |
+ ------ +
| 1 | 1 | aa |
| 1 | 1 | aa |
| 2 | 2 | bb |
| 3 | 3 | cc |
| 4 | 4 | dd |
| 5 | 5 | ee |
| 5 | 5 | EE |
+ ------ +
7 rows in set (0.00 sec)
Mysql> select * from user_id a join user_profile B on a. id = B. id;
+ ------ +
| Id | name |
+ ------ +
| 1 | 1 | aa |
| 1 | 1 | aa |
| 2 | 2 | bb |
| 3 | 3 | cc |
| 4 | 4 | dd |
| 5 | 5 | ee |
| 5 | 5 | EE |
+ ------ +
7 rows in set (0.00 sec)

I hope this article will help you with MySQL program design.

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.