max_
max_

Reputation: 24491

Query fails if no rows are found

I have the following query which returns NULL for each field if there are no matching questions or answers for the user. Please can you tell me how I can prevent this from happening?

Thanks!

SELECT u.id, u.first_name, u.last_name, u.email, u.username, COUNT( DISTINCT q.id ) AS  `q_count` , COUNT( DISTINCT a.id ) AS  `a_count` 
FROM  `users` AS u
INNER JOIN  `questions` AS q ON u.id = q.uid
INNER JOIN  `answers` AS a ON u.id = a.uid
WHERE u.username =  'max'
LIMIT 0 , 30

Upvotes: 2

Views: 46

Answers (1)

a1ex07
a1ex07

Reputation: 37382

Use left joins instead of inner joins

Upvotes: 4

Related Questions