I have these tables.
PROF TBL:
------------------------------------
| pf_id | pf_parent_id | pf_foobar |
------------------------------------
| 1 | NULL | lipsum | -> root -> count(2) aggregate(5)
| 2 | NULL | lipsum | -> root
| 3 | 1 | lipsum | -> descendant
| 4 | 1 | lipsum | -> descendant -> count(0) aggregate(3)
| 5 | 4 | | -> descendant -> count(1) aggregate(3)
| 6 | 5 | | -> leaf -> count(2) since leaf(2)
------------------------------------
USER TBL:
---------------------------------------
| user_id | pf_id (ref) | user_foobar |
---------------------------------------
| 1 | 1 | qwe |
| 2 | 1 | |
| 3 | 5 | |
| 4 | 6 | |
| 5 | 6 | qwe |
---------------------------------------
I intend to COUNT()
every instance of the pf_id
found in usertbl
I get that fine for every distinct pf_id with this query.
SELECT proftbl.pf_id, proftbl.pf_parent_id, proftbl.pf_title, COUNT(proftbl.pf_id),
FROM `proftbl`
LEFT JOIN `usertbl` ON `usertbl`.pf_id=`proftbl`.pf_id
GROUP BY `proftbl`.pf_id;
BUT I'd like to to aggregate the count of pf_id
Records having descendants.
I don't know how to go about this. Any thoughts?
I like to write queries this way; I think it makes it more obvious where errors occur...
SELECT proftbl.pf_id
, proftbl.pf_parent_id
, proftbl.pf_title
, COUNT(proftbl.pf_id)
, -- !!!!!!!!!! SURELY SOME MISTAKE HERE !!!!!!!!!!!!
FROM `proftbl`
LEFT
JOIN `usertbl`
ON `usertbl`.pf_id = `proftbl`.pf_id
GROUP
BY `proftbl`.pf_id;