I have table 1 that represents Items with column ItemID and column ItemInfo.
Then, I have table 2 that represents SubItems with column ItemID (the parent), column SubItemID and column SubItemInfo.
I’m trying to get the count of all subitems for each item with this :
select items.itemID, count(*) as count
from items, subItems
where subItems.itemID=items.itemID group by itemID
It works fine except for items that don’t have sub-items. Instead of returning the item row with a sub-item count of 0, the row is simply not there.
Is there an efficient way to force all rows from the first table (Items) to be selected ?
You neet to use left outer join, than you’ll get 0 for items having no subitems, like that:
If you wanted to have better performance, you could try this query:
BUT, with this one you will not get any info about the items, that have no subitems (they are simply not present within subitems table).