MySQL: Count then sort by the count total -
i know other posts talk this, haven't been able apply situation.
this have far.
select * ccparts, ccchild, ccfamily pargc = '26' , parchild = chiid , chifamily = famid order famname, chiname
what need see total number of ccparts same ccfamily in results. then, sort total.
it looks close want:
select f.famid, f.famname, pc.parcount ( select c.chifamily famid, count(*) parcount ccparts p join ccchild c on p.parchild = c.chiid p.pargc ='26' group c.chifamily ) pc join ccfamily f on f.famid = pc.famid order pc.parcount
the inline view (between parentheses) headliner: grouping , counting. note not need join table ccfamily
there group family, table ccchild
carries family information. if don't need family name (i.e. if id sufficient), can stick inline view alone, , there order count(*)
. outer query associates family name results.
additionally, mysql provides non-standard mechanism combine outer query inline view, in case doesn't either clarity or concision. query provided should accepted sql implementation, , it's advantage learn such syntax , approaches first.
Comments
Post a Comment