Zabihullah Alipour
Zabihullah Alipour

Reputation: 635

Query return null instead of a result with some record

I'm sorry, perhaps this question is simple but I can't solve it.

I have 3 tables:

Now these tables have these records:

I 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

Answers (1)

xdazz
xdazz

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

Related Questions