Join filter considerations for MySQL SQL statements

Source: Internet
Author: User

We are not unfamiliar with the join statements for SQL statements. This is noteworthy for content filtering in the where statement, or for filtering in the on statement or having.

Case: To find a period of time to register new users, not through the Novice task specific which step task, statistics

which involves two tables

User base table: user_base, user_id, registration time Created_at

Task table: User_id,task_id,task_status

A statement:

Select min (b.task_id) life,task_status, a.user_id from User_tasks b join (select user_id from user_bases where creat Ed_at between ' and ') A on a.user_id=b.user_id group by A.USER_ID has b.task_status<3;

B Statement:

Select min (task_id), task_status,a.user_id from User_tasks b join (select user_id from user_bases where Created_at betwee n ' and ') A on a.user_id=b.user_id and task_status<3 Group by USER_ID;

The biggest difference between A,B statements is the order in which task_status<3 is filtered.

A, the statement in the table content to link match, take user_id, find each user's task_id, finally is having task_status<3 filter

B, when the statement is a table content match, Task_status<3 joins and, according to group by, asks each user min task_id.

So the B statement is in line with the standard.

When you link to a join, decide which fields to filter the content in!

This article is from the "Technology Achievement Dream" blog, please be sure to keep this source http://weipengfei.blog.51cto.com/1511707/1117069

See more highlights of this column: http://www.bianceng.cnhttp://www.bianceng.cn/database/MySQL/

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.