I've got a few tables. The first one is named category
, here it is:
id|template_id|name |entry_count|is_base_template|can_rename|can_delete|section|userId|parentCategoryId|
--|-----------|-------|-----------|----------------|----------|----------|-------|------|----------------|
1| |Notes | 0|true |true |true |A | | |
2| |ToDo | 0|true |true |true |A | | |
3| 1|Notes | 0|false |true |true |A | 1| |
4| 2|ToDo | 0|false |true |true |A | 1| |
5| |Must Do| 0|false | | |A | | 4|
The second table is called entry
:
id|body |title |createdOn |startDate|finishDate|isArchived|isLocked|isTrashed|
--|---------------------------------------------|--------------------|-------------------|---------|----------|----------|--------|---------|
1|im a root entry |root entry |2020-06-11 22:40:01| | | | | |
2|I must do it |must do title |2020-06-11 22:42:16| | | | | |
3|i was asked to do it right now and right here|one more must do |2020-06-11 17:44:22| | | | | |
4|5 subcat body |5 subcat title |2020-06-11 17:46:47| | | | | |
5|5 subcat body try 2 |5 subcat title try 2|2020-06-11 17:51:26| | | | | |
6|5 subcat body try 3 |5 subcat title try 3|2020-06-11 17:53:17| | | | | |
7|4 subcat body try 1 |4 subcat title try 1|2020-06-11 17:54:34| | | | | |
There's also the table of relations (category_entries_entry):
categoryId|entryId|
----------|-------|
4| 1|
5| 5|
5| 6|
4| 7|
A category can possess children, if the parentCategoryId is not NULL then we're dealing with its child. For instance, the fifth category (id = 5) is actually a subcategory of the forth category. One of the requirements at the moment is that children can't have their own children.
What I need is to count the number of entries on each category, regardless of how many subcategories a category has.
If I do this:
SELECT category.id as cat_id , COUNT(*) as entries_in_cat
FROM category
LEFT JOIN category_entries_entry
ON category.id = category_entries_entry."categoryId"
LEFT JOIN entry
ON entry.id = category_entries_entry."entryId"
WHERE category.is_base_template = false
GROUP BY category.id;
Here is what I get:
cat_id|entries_in_cat|
------|--------------|
5| 2|
4| 2|
3| 1|
The problem is that the category with id = 5 is a subcategory, which means it should be added to what is in the category with id = 4 cos that's her parentCategory id. So the table for this particular case should look like this:
cat_id|entries_in_cat|
------|--------------|
4| 4|
3| 1|
Hence the question: How do I achieve that?