MySQL learning two about left connection

Source: Internet
Author: User

The work has the following SQL, for a.id = ' ABCDEFG ', left join B and C two tables, to find additional information. Even if there are no records in B and C that meet the criteria, the final result is definitely not empty. Because a.id = ' ABCDEFG ' is present.

SELECT ****** fromTableA A Left JOINTableB B onb.deleted= '0' anda.deleted= '0' andB.fid=a.id Left JOINTableC C onB.xxxid=C.id andc.deleted= '0'WHEREa.ID='ABCDEFG'

The first thing I'm writing is this: put b.deleted = ' 0 ' behind, which results in NULL, because: if you put and b.deleted = ' 0 ' At the end, it is the result of the left join filtered,

At this point it is possible to cause the result to be null. (Consider this: the B and C tables do not have a successful record with a connection, if you use the SQL above, will result, using this SQL, will be null)

SELECT ****** fromTableA A Left JOINTableB B on anda.deleted= '0' andB.fid=a.id Left JOINTableC C onB.xxxid=C.id andc.deleted= '0'WHEREa.ID='ABCDEFG'  andb.deleted= '0'

Above this problem is encountered in the work, accidentally wasted a lot of time to fix, SQL Master bad!

MySQL learning two about left 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.