MySQL uses the leftjoin method when the data in the right table is not unique _ MySQL

Source: Internet
Author: User
This article describes how to use the leftjoin method when data in the right table is not unique in MySQL, if the number of records that meet the condition expression in the right table is greater than one record, you can refer 1. basic left join usage

Mysql left join statement format
A left join B ON condition expression

Left join is based on table A. Table A is the left table, and Table B is the right table.
All records in the left table (A) are displayed, while in the right table (B), only records meeting the condition expression are displayed. If no matching record exists in the right table (B, NULL is the place where the record is insufficient.

For example, the structure of the news and news_category tables is as follows. the category_id of The news table corresponds to the id of the news_category table.

News table

News_category table

Displays the news Table record and news category name. The query statement is as follows:

select a.id,a.title,b.name as category_name,a.content,a.addtime,a.lastmodify  from news as a left join news_category as b  on a.category_id = b.id;

The query result is as follows:

Because the news_category table does not have a record with id = 4, category_name = NULL for the record with category_id = 4 in the news table

When left join is used, the number of records displayed in Table A and Table B is OR. all records in Table A are displayed, and only matching records are displayed in Table B.

2. left join right table data is not unique solution

However, if the number of qualified records in Table B is greater than 1, 1: n will occur. as A result, the number of records after left join is greater than that in Table.

For example, the structure of the member and member_login_log tables is as follows. member records the member information and member_login_log records the member's daily logon records. The id of the member table corresponds to the uid of the member_login_log table.

Member table

Member_login_log table

Query the member user information and the last logon date:
If left join is used directly

select a.id, a.username, b.logindate from member as a  left join member_login_log as b on a.id = b.uid;

Because member_login_log has more matching records than the member table (a. id = B. uid), the final record is:

However, this is not the expected result. Therefore, we need to ensure that the qualified records of Table B are null or unique. we can use group by to implement this.

select a.id, a.username, b.logindate from member as a  left join (select uid, max(logindate) as logindate from member_login_log group by uid) as b on a.id = b.uid;

Conclusion: the relationship between the two tables that use left join is preferably or. This ensures that all the records in Table A are displayed, while those in Table B are qualified.
If the qualified records in Table B are not unique, check whether the table design is reasonable.

The above is the content of the left join method _ MySQL when the data in the right table is not unique. For more information, see The PHP Chinese network (www.php1.cn )!

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.