nfroidure
nfroidure

Reputation: 1601

LEFT JOIN optimizations

I'm wondering if it's better to do joined query like that :

SELECT * FROM table1
    LEFT JOIN table2 ON table1.field1=table2.id
    AND table2.field2="mystring"
WHERE table2.field2="mystring"

Instead of simply doing it :

SELECT * FROM table1
    LEFT JOIN table2 ON table1.field1=table2.id
WHERE table2.field2="mystring"

Is the MySQL engine doing this optimizations for me or is it better to do it explicitly ?

Does :

SELECT * FROM table1
    JOIN table2 ON table2.field2="mystring" AND table2.id=table1.field1

has the same effect, will results be identical ?

Edit: Some more precisions, my questions are :

Upvotes: 1

Views: 88

Answers (1)

Charlie Vieillard
Charlie Vieillard

Reputation: 792

This will still show records of table1 if table2.field2!="mystring"

SELECT * FROM table1
    LEFT JOIN table2 ON table1.field1=table2.id
    AND table2.field2="mystring"

This will not show records of table1 if table2.field2!="mystring"

SELECT * FROM table1
    LEFT JOIN table2 ON table1.field1=table2.id
WHERE table2.field2="mystring"

The output can be different. The first query in this situation could show more results then the second.

Upvotes: 2

Related Questions