Considerations for connecting SQL statements to on conditions

Source: Internet
Author: User

Select * From view_vehiclecontractinfo VCI with (nolock)
Left join auctionvehicle av with (nolock)
On VCI. vehiclecontractid = Av. vehiclecontractid
And av. auctionid = (select max (auctionvehicle. auctionid) from auctionvehicle where auctionvehicle. vehiclecontractid in ('+ @ vehiclecode + '))
Left join view_vehicleserviceinfo as
On VCI. vehiclecode = vs. vehiclecode
And vs. servicecustomertypecode ='s''
And vs. servicesettleprice <> 0 and vs. auctionid = Av. auctionid
Left join storemanagement as Sm on VCI. vehiclecode = Sm. gpcode
Where VCI. vehiclecontractid in ('+ @ vehiclecode + ')

For example, if the above Code does not have a value when connecting left and right, it is replaced by null, which invalidates the conditions unrelated to the connection under on.

For example, 1. And vs. servicecustomertypecode ='s''

2. Av. auctionid = (select max (auctionvehicle. auctionid) from auctionvehicle where auctionvehicle. vehiclecontractid in ('+ @ vehiclecode + '))

3. And vs. servicesettleprice <> 0

These three conditions are not related to the connection. They are only filter conditions. Therefore, you must add them to the WHERE clause instead of put them in the on clause.

 

When innner joins, some filtering conditions in on are not ignored because they are strictly connected.

 

From: http://beijiguang-fred.blog.sohu.com/153417642.html

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.