我有一个包含嵌套集模型类别的表格,我想根据他们拥有的产品数量来分类。像这样的东西
Categories-(6)
Cars-(4)
BMW-(2)
Opel-(1)
Mercedes-(1)
Trucks-(2)
Man-(1)
Mercedes-(1)
我有两个表,categories
和types
分类:id,name,level,lft,rgt
类型:id,category_id,name
现在我只能列出这个类别:
$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();
无论如何都要像上面那样返回计数?
答案 0 :(得分:0)
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;
你会得到
BMW-(2)
Opel-(1)
Mercedes-(1)
来自上面的查询
for Cars-(4) 你只需要这样做
select count(*) as numberOfCars from cars;