Reputation: 7094
I have 2 tables.
workers and experience
In table workers there is row worker_parent (because one worker can be responsible for other).
No I must connect tables like that:
SELECT w1.* FROM workers w1 LEFT JOIN workers w2 ON (w2.id = w1.worker_parent)
And that's ok. But I have to order by experience of w2 and I try to add table experience but it's connect to w1 and not to w2.
My question is how to add table experience (which has row worker_id) to w2 and order by experience AND SELECT data from w1.
This is my try.
SELECT w1.* FROM workers w1 LEFT JOIN workers w2 ON (w2.id = w1.worker_parent) LEFT JOIN experience e ON (w2.id = e.worker_id) ORDER BY e.experience DESC
Thank's for help
Upvotes: 1
Views: 66
Reputation: 5846
if you only want child workers that have a parent with experince, you need to add a where (or change join type) (for readability i renamed w1 and w2 to w_child and w_parent)
SELECT w_child.*, e_parent.experience
FROM workers AS w_child
LEFT JOIN workers AS w_parent ON (w_parent.id = w_child.worker_parent)
LEFT JOIN experience AS e_parent ON (w_parent.id = e_parent.worker_id)
WHERE e_parent.worker_id IS NOT NULL
ORDER BY e_parent.experience DESC
Upvotes: 1