I have a table of categories:

```
id,label,parent_id
1,Animals,null
2,Dogs,1
3,Cats,1
4,Black,3

```

This means that "Animals" is a top-level category, and "Dogs" and "Cats" are both sub-categories to "Animals", and "Black" is a sub-category of "Cats" (itself a sub-category of "Animals"). It can be any number of levels/"generations".

I frankly never expected this to be any kind of issue, but once I wanted to create a query to count all the objects in the "Animals" category (meaning also all in the sub-categories), I hit a hopeless dead end. Both the PostgreSQL manual and the Stack Exchange questions and the other online resources just show these massive, confusing `WITH RECURSIVE` queries full of `UNION`s and everything. I simply cannot follow them.

It cannot possibly be this difficult. I must be misunderstanding something.

All I try is to do is:

```
SELECT count(*) FROM table WHERE id = $1 OR parent_id RECURSIVE = $1;

```

And:

```
SELECT id FROM table WHERE id = $1 OR parent_id RECURSIVE = $1;

```

Naturally, I have made up the `RECURSIVE` keyword here, but that's how I expected it to work.

I've read the manual and the Stack Exchange answer numerous times now and I just have no idea how I'd "adapt" it to my situation. I don't even understand what they are doing in the examples. It's simply too damn complex. This seems like such a common problem that it must have been long since elegantly solved... right?