I have a user system where user accounts can own subordinate accounts.
To express this relationship I have the following mysql tables:
TABLE ACCOUNTS
ID
name
TABLE OWNEDACCOUNTS
accountID -> ACCOUNTS(ID)
ownerID -> ACCOUNTS(ID)
Though it isn’t obvious from these tables, this relationship can only go 2 levels deep:
Account -> Sub-Account -> Sub-Sub-Account
Currently I am able to SELECT all of the sub-accounts that belong to a top level account with the following:
SELECT a.ID
FROM accounts AS a
JOIN ownedAccounts AS o
ON o.accountID = a.ID
AND o.ownerID = ?
However, what I’d really like is to SELECT all of the sub-accounts AND all of the sub-sub accounts that belong to a single top level account. How do I do this?
Thanks (in advance) for your help
1 Answer