The difference between on and where in MySQL after join

Source: Internet
Author: User
Tags joins

SELECT *  from A

SELECT *  from B

These are the two-sheet bodies.

SELECT *  from  Left JOIN  on a.ID=b.a_id;

statement One   :   SELECT*from leftjoins on a.id=and  <3;

Statement two:  SELECT*from leftjoins on a.id=WHERE  <3;

The results of the two-statement query are different.

The keyword order for SQL queries is from, where, group by-> have->order by

The left join is in the from range so first on condition filter, then left JOIN

And where is filtered again after the result of the left join

Statement one (equivalent to select *from A left JOIN (SELECT * from B WHERE b.id < 3) b2 on a.id = b2.a_id;)

    1. Filter b.id<3 by on condition
    2. 1 query results with a LEFT join

Statement Two

    1. Left JOIN first
    2. Then filter again based on where conditions

Precautions for use:

    1. The filter criteria behind on are primarily for the associated table (the filter for the primary table does not apply)
    2. The filter criteria for the primary table should be placed behind the where and should not be placed behind
    3. If the query criteria query after the join connection should put the query conditions on the back; if you want to connect after the filter should put the condition behind where
    4. For association tables, you can do a subquery and join

The difference between on and where in MySQL after join

Related Article

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.