Reputation: 635
I'm sorry, perhaps this question is simple but I can't solve it.
I have 3 tables:
users
(id, name
)resumes
(id, title, users_id_fk
)comments
(id, comment, resumes_id_fk
)Now these tables have these records:
users
{ (1, N1), (2, N2)} ==> 2 usersresumes
{ (1, title1, 1), (2, title2, 1)} ==> 2 resumescomments
{(1, Comment, 2)} ==> 1 commentI want to execute a query that returns title and count of comments for a specific user resume:
SELECT
u.name, r.title, count(c.comment)
from
users u, resumes r, comments c
where
c.id = r.id
and r.id = u.id
and u.id = 2
group by
u.name, r.title;
Problem is that my excepted result is: {(N2, ,0)}
But returned {(,,)}
My SQL is not good, pls.
Upvotes: 0
Views: 45
Reputation: 160973
You are using INNER JOIN, change to LEFT JOIN will fix your problem.
SELECT u.name, r.title, count(c.comment)
from users u
left join resumes r on r.users_id_fk =u.id
left join comments c on c.resumes_id_fk = r.id
where u.id = 2
group by u.name, r.title
And your join condition is wrong too.
Upvotes: 5