Reputation: 1601
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 :
i assume the 3 above queries are equivalent in term of given results. Are they really (especially the third) ?
i'm wondering if the engine treat them equally in terms of performances (in other words do i have to worry wich one to use) ?
Upvotes: 1
Views: 88
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