![](/img/trans.png)
[英]How can I find which category has the most products shipped and the net income from sales in that category?
[英]How can I count products in category
我有一个表格,其中包含嵌套集模型中的类别,我想将类别与它们拥有的产品数量进行比较。 像这样的东西
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();
无论如何,有没有要返回上面的那个计数?
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)
从上面查询
对于汽车-(4),您只需要执行此操作
select count(*) as numberOfCars from cars;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.