joining 5 tables to obtain all comments made about goal where id = 2
I need to return all comments that were made on a goal where id = 2
Tables:
goals.id = objectives.goal_id
objectives.id = steps.objective_id
steps.id = transactions.step_id
comments.id = transactions.comment_id
transactions table keeps track of all the steps a comment was related to.
Each comment can be related to multiple steps, therefore a table (transactions - I know, bad naming convention on this table) to keep track of each step and the comment.
So the transactions table is set up like this:
transactions.step_id | transactions.comment_id
So the user would like to 开发者_开发技巧view all comments that relate to goal where id = 2
I have never done anything over 2 tables, so any help is appreciated.
select c.*
from goal g
inner join objectives o on g.id = o.goal_id
inner join steps s on o.id = s.objective_id
inner join transaction t on s.id = t.step_id
inner join comments c on t.comment_id = c.id
where g.id = 2
That's what i was writing :)
One thing if the relations are no obligatory maybe need to use left join in some cases
精彩评论