I have a table with categories in nested set model, I would like to take the categories with how many products they have. something like this
Categories-(6)
Cars-(4)
BMW-(2)
Opel-(1)
Mercedes-(1)
Trucks-(2)
Man-(1)
Mercedes-(1)
I have two tables, categories
and types
Categories: id,name,level,lft,rgt
Types: id,category_id,name
Now I'm able to list only the categories with this:
$categories = Doctrine_Core::getTable('Category')
->createQuery('c1')
->select('c1.id, c1.level, c1.name')
->innerJoin('c1.Category c2 ON ( c1.lft BETWEEN c2.lft AND c2.rgt )')
->andWhere(' c2.id = ?', $id)
->andWhere('c1.level > 0')
->andWhere('c1.level < c2.level+3')
->groupBy('c1.id')
->orderBy('c1.lft')
->execute();
Is there anyway to return the count like that one above?
select table1.name,table2.countcars
from categories as table1 right join
(select category_id,count(*) as countcars
from cars group by category_id) as table2
on table1.id=table2.category_id;
You'll get
BMW-(2)
Opel-(1)
Mercedes-(1)
from above query
for Cars-(4) you'll simply have to do this
select count(*) as numberOfCars from cars;
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.