mysql - How to multiple join tables? -


i have 2 tables.

workers , experience

in table workers there row worker_parent (because 1 worker can responsible other).

no must connect tables that:

select w1.* workers w1 left join workers w2 on (w2.id = w1.worker_parent) 

and that's ok. have order experience of w2 , try add table experience it's connect w1 , not w2.

my question how add table experience (which has row worker_id) w2 , order experience , select data w1.

this try.

select w1.* workers w1 left join workers w2 on (w2.id = w1.worker_parent) left join experience e on (w2.id = e.worker_id) order e.experience desc 

thank's help

if want child workers have parent experince, need add (or change join type) (for readability renamed w1 , w2 w_child , w_parent)

select w_child.*, e_parent.experience workers w_child left join workers w_parent on (w_parent.id = w_child.worker_parent) left join experience e_parent on (w_parent.id = e_parent.worker_id) e_parent.worker_id not null order e_parent.experience desc 

Comments

Popular posts from this blog

javascript - backbone.js Collection.add() doesn't `construct` (`initialize`) an object -

php - Get uncommon values from two or more arrays -

Adding duplicate array rows in Php -