colinsp Posted March 22, 2009 Share Posted March 22, 2009 Hi I am fairly new to php and mySQL I have the following query SELECT `type`.id, `type`.category, `type`.description, Count(item.category_id) FROM `type` Inner Join item ON `type`.id = item.category_id GROUP BY `type`.id ORDER BY `type`.category ASC This returns the results I expect where there are items in the type table that correspond to a category_id BUT I want this query to also return 0 values of category. The result that I want would be something like: Bridge 1 Compact 0 DSLR 3 but all I am getting is Bridge 1 DSLR 3 Any pointers most welcome. I have spent a couple of hours searching Google without success. TIA -- Colin Quote Link to comment https://forums.phpfreaks.com/topic/150558-solved-how-do-i-count-0s-in-a-group-by-query/ Share on other sites More sharing options...
Mchl Posted March 22, 2009 Share Posted March 22, 2009 Do you have a table, where all categories are stored? Quote Link to comment https://forums.phpfreaks.com/topic/150558-solved-how-do-i-count-0s-in-a-group-by-query/#findComment-790833 Share on other sites More sharing options...
colinsp Posted March 22, 2009 Author Share Posted March 22, 2009 Hi Yes the type table stores the category and its description the item table stores the items and is joined by item.category_id to type.id Quote Link to comment https://forums.phpfreaks.com/topic/150558-solved-how-do-i-count-0s-in-a-group-by-query/#findComment-790842 Share on other sites More sharing options...
Mchl Posted March 22, 2009 Share Posted March 22, 2009 Ok then Try changing INNER JOIN to LEFT JOIN Quote Link to comment https://forums.phpfreaks.com/topic/150558-solved-how-do-i-count-0s-in-a-group-by-query/#findComment-790868 Share on other sites More sharing options...
colinsp Posted March 22, 2009 Author Share Posted March 22, 2009 Brilliant. Thanks that works fine I must do some more reading about joins. Quote Link to comment https://forums.phpfreaks.com/topic/150558-solved-how-do-i-count-0s-in-a-group-by-query/#findComment-790880 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.