Questions about the left join that don't work when you encounter where

Source: Internet
Author: User

Today to do a stored procedure, the function is to query everyone's score, some people do not give 0 points, it is obvious here with the left outer connection is no problem,

But after the connection has a time-based filtering function, so I added the where condition to judge, this time did not think of things happen, the role of Left outer connection does not

, and become the same as the internal connection query, no score of the people will not be queried out, and then asked the person to think about it, left outer connection query is a result set, also

Can be said to be a temporary table, plus where after the query out based on the filter once, naturally there is no condition of the record. The correct treatment should be to put on the back of the

where you switch to and to query when you connect, here's an example.

There are two tables below

1.stu Student Table

2.score score Table

Compare the following two different types of queries!

SELECT S. ' Name ', Ifnull (c. ' Score ', 0) as score from Stu S left joins score C on S. ' id ' = c. ' stu_id ' WHERE score > 50

This is the first query out the result set and then filter, naturally no less than 50 records

SELECT S. ' Name ', Ifnull (c. ' Score ', 0) as score from Stu S left joins score C on S. ' id ' = c. ' stu_id ' and score > 50

This is the first filter query out of the result set, you can query out all the records

Questions about the left join that don't work when you encounter where

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.