In what order are MySQL JOINs evaluated?

后端 未结 7 1333
情书的邮戳
情书的邮戳 2020-11-30 09:26

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         


        
相关标签:
7条回答
  • 2020-11-30 10:28

    Im not sure about the ON vs USING part (though this website says they are the same)

    As for the ordering question, its entirely implementation (and probably query) specific. MYSQL most likely picks an order when compiling the request. If you do want to enforce a particular order you would have to 'nest' your queries:

    SELECT c.*
    FROM companies AS c 
        JOIN (SELECT * FROM users AS u 
            JOIN (SELECT * FROM  jobs AS j USING(userid) 
                  JOIN useraccounts AS us USING(userid) 
                  WHERE j.jobid = 123)
        )
    

    as for part 4: the where clause limits what rows from the jobs table are eligible to be JOINed on. So if there are rows which would join due to the matching userids but don't have the correct jobid then they will be omitted.

    0 讨论(0)
提交回复
热议问题