mysql - SubQuery on Single Table -
mysql - SubQuery on Single Table -
ok have table
tasks -- taskid (unique autoinc primary) childof (contains task id of parent, or 0 if top tier (no parent))
i need write query selects records childof = 0 ... simple right ?
ok need have column returned results tells how many children each task has ...
so result ...
taskid ... childof ... countchildren 37 ...... 0 .... 3 42 ...... 0 .... 0 99 ...... 0 .... 1 etc....
i know 2 queries need this, need combine them somehow...
select taskid parenttaskid, childof tasks childof = 0
and
select count(taskid) tasks childof = parenttaskid
note: there 2 tiers.. parent , kid ... no grandchildren! makes bit less complicated.
any help appreciated. help far!
something should it:
select taskid parenttaskid, childof , (select count(t2.taskid) tasks t2 t2.childof = t.taskid) countchildren tasks t t.childof = 0
mysql sql subquery hierarchical-data
Comments
Post a Comment