Skip to main content
added 1 character in body
Source Link
Cid
  • 15.3k
  • 4
  • 33
  • 49

Because the whole result is filtered against B.id_A = SpecificPart removing the parts B.id_A =IS NULL, that are in the A that aren't in B

Because the whole result is filtered against B.id_A = SpecificPart removing the parts B.id_A = NULL, that are in the A that aren't in B

Because the whole result is filtered against B.id_A = SpecificPart removing the parts B.id_A IS NULL, that are in the A that aren't in B

Source Link
Cid
  • 15.3k
  • 4
  • 33
  • 49

Let's consider those tables :

A

id | SomeData 

B

id | id_A | SomeOtherData 

id_A being a foreign key to table A

Writting this query :

SELECT * FROM A LEFT JOIN B ON A.id = B.id_A; 

Will provide this result :

/ : part of the result B +---------------------------------+ A | | +---------------------+-------+ | |/////////////////////|///////| | |/////////////////////|///////| | |/////////////////////|///////| | |/////////////////////|///////| | |/////////////////////+-------+-------------------------+ |/////////////////////////////| +-----------------------------+ 

What is in A but not in B means that there is null values for B.


Now, let's consider a specific part in B.id_A, and highlight it from the previous result :

/ : part of the result * : part of the result with the specific B.id_A B +---------------------------------+ A | | +---------------------+-------+ | |/////////////////////|///////| | |/////////////////////|///////| | |/////////////////////+---+///| | |/////////////////////|***|///| | |/////////////////////+---+---+-------------------------+ |/////////////////////////////| +-----------------------------+ 

Writting this query :

SELECT * FROM A LEFT JOIN B ON A.id = B.id_A AND B.id_A = SpecificPart; 

Will provide this result :

/ : part of the result * : part of the result with the specific B.id_A B +---------------------------------+ A | | +---------------------+-------+ | |/////////////////////| | | |/////////////////////| | | |/////////////////////+---+ | | |/////////////////////|***| | | |/////////////////////+---+---+-------------------------+ |/////////////////////////////| +-----------------------------+ 

Because this removes in the inner join the values that aren't in B.id_A = SpecificPart


Now, let's change the query to this :

SELECT * FROM A LEFT JOIN B ON A.id = B.id_A WHERE B.id_A = SpecificPart; 

The result is now :

/ : part of the result * : part of the result with the specific B.id_A B +---------------------------------+ A | | +---------------------+-------+ | | | | | | | | | | +---+ | | | |***| | | | +---+---+-------------------------+ | | +-----------------------------+ 

Because the whole result is filtered against B.id_A = SpecificPart removing the parts B.id_A = NULL, that are in the A that aren't in B