如何获得下表的结果。
catTbl
catId, name
11 fruit
12 vegetable
prodTbl
pId | catTbl_catId | name
1 11 apple
2 11 orange
3 12 slag
我想获得
的结果 cat name total count
fruit 2
vegetable 1
按总计数排序 -
如何在cakephp 3查询下使用总计订单..
$cats=$this->catTbl->find()
->where(['catTbl.status'=>'1','is_deleted'=>'0'])
->select(['name','catId','modified'])
->contain([
'prodTbl' => function($q) {
$q->select([
'prodTbl.catTbl_catId',
'total' => $q->func()->count('prodTbl.catTbl_catId')
])
->where(['prodTbl.status'=>'1','prodTbl.is_deleted'=>'0'])
->group(['prodTbl.catTbl_catId']);
return $q;
}
]);
答案 0 :(得分:0)
我设法得到了结果
+------------+---------+
| Cat | Total |
+------------+---------+
| fruit | 2 |
| vegetable | 1 |
+------------+---------+
使用此查询
$query = $this->Prod->find();
$cats = $query->select([
'Cat.name',
'total' => $query->func()->count('catId')
])
->contain([
'Cat'
])
->group([
'catId'
])
->order([
'total' => 'DESC'
])
->all();