I have the following query:
SELECT c.*
FROM companies AS c
JOIN users AS u USING(companyid)
JOIN jobs AS j USING(userid)
JOIN useraccounts AS us USING(userid)
WHERE j.jobid = 123;
I have the following questions:
- Is the USING syntax synonymous with ON syntax?
- Are these joins evaluated left to right? In other words, does this query say: x = companies JOIN users; y = x JOIN jobs; z = y JOIN useraccounts;
- If the answer to question 2 is yes, is it safe to assume that the companies table has companyid, userid and jobid columns?
- I don't understand how the WHERE clause can be used to pick rows on the companies table when it is referring to the alias "j"
Any help would be appreciated!
Here is a more detailed answer on
JOIN
precedence. In your case, theJOIN
s are all commutative. Let's try one where they aren't.Build schema:
Add data:
Run query:
Result:
Only the Bob row is returned
Analysis:
In this query the
LEFT OUTER JOIN
was evaluated first and theJOIN
was evaluated on the composite result of theLEFT OUTER JOIN
.Second query:
Result:
One row for Bob (with the fulfillment data) and one row for Mary with NULLs for fulfillment data.
Analysis:
The parenthesis changed the evaluation order.
Further MySQL documentation is at https://dev.mysql.com/doc/refman/5.5/en/nested-join-optimization.html