When SQL query join order problem

         In our work often write queries, then left join and right join is a common association query statement. But when we use them, you have not thought about how to do association, how to choose their order it?

For example: When A table associated with Table B, Table C A also associated association, A and B, there may be a Null value when A and B association, also appears to allow Null, A and C association can not have a Null value, then we first they should think about how to deal with it?

You might say this is not simple, ah, as follows:

Achieve 1:

select * from C inner join A 

on C.ID=A.ID

Left Join B

on A.Code=B.Code

Achieve 2:

select * from A Left Join B

on A.Code=B.Code

inner join C

on A.ID=C.ID

Achieve 3:

select * from B Right Join A

on A.Code=B.Code

inner join C

on A.ID=C.ID

This 3 way just listed, it has been relatively good?

Under the current situation, if a considerable amount of data, do first inner join, then

left join and right join can provide such efficient query.

But also with the actual situation. In general, we would prefer to achieve 1.

Reproduced in: https: //www.cnblogs.com/kevinGao/archive/2012/09/17/2776041.html

Guess you like

Origin blog.csdn.net/weixin_34293059/article/details/93359588