Mission
I'm trying to find out the count of children in a set of tables illustrated below. The enviroment is LAMP but help in the right direction via other syntaxes are appreciated.
Table structure
users
-----
user_id
parent_id
user_meta
---------
user_id
registration_date
user_levels
-----------
user_id
level
This basic structure is unlikely to change but could be extended.
Use case
select
users.user_id
from
users
inner join
user_meta
on users.user_id = user_meta.user_id
inner join
user_levels
on users.user_id = user_levels.user_id
where
parent_id = *x*
and
registration_date > *certain date*
and
level < *certain level*
Conditions
- A user's descendant only counts as such if its level is lower than given
*certain level*
. If descendant's level is not lower, the node is a leaf but should be excluded from the count. - Given
*certain level*
and*certain date*
are the same for every query/set of queries.
I've tried using this in a loop but the amount of queries quickly escalades. This solution could probably be used and stored in a cron job but I'd prefer an as-real-time-as-it-gets-solution.
(PS: since this is my first question, feel free to edit and give hints on how to ask better questions)