Frank Nwoko
Frank Nwoko

Reputation: 3934

Count number of articles in categories

This code only shows categories with articles in them. I want to show all categories. Pls help.

$query = "SELECT C.id, C.jcat_name,COUNT(A.catid) AS catid FROM jt_categories C INNER JOIN jt_articles A ON C.id = A.catid GROUP BY C.id";

Upvotes: 0

Views: 331

Answers (5)

Albanicus
Albanicus

Reputation: 1

Did u try LEFT JOIN ? bc. (i think) in second table u have NULL articles for some categories.

Upvotes: 0

BeemerGuy
BeemerGuy

Reputation: 8269

Change INNER JOIN to LEFT JOIN.

Upvotes: 1

Haim Evgi
Haim Evgi

Reputation: 125466

change to left join

 SELECT C.id, C.jcat_name,COUNT(A.catid) AS catid FROM jt_categories C 
LEFT JOIN jt_articles A ON C.id = A.catid GROUP BY C.id

Upvotes: 1

dcarneiro
dcarneiro

Reputation: 7150

replace the inner join by a left outer join

Upvotes: 1

Rodolphe
Rodolphe

Reputation: 497

Change INNER JOIN for LEFT JOIN in your query.

INNER JOIN looks explicitely for the join in the data

Upvotes: 1

Related Questions