Cake 3x:-如何在find查询中使用count和sum



我想在cakephp-3.x中选择count"id"和价格总和简单的mysql查询:-

SELECT COUNT(id) as count, SUM(price) as total_price FROM bookings WHERE id=3;

我不知道如何在cakephp-3.x 中编写这个查询

请让我知道如何在cakephp-3.x 中编写此查询

首先尝试阅读如何使用ORM:的查询生成器

查询生成器-聚合组和具有

这将是

$query = $bookings->find();
$query->select([
    'count' => $query->func()->count('id'),
    'total_price' => $query->func()->sum('price')
])->where(['id' => 3]); 

计数将来自结果数,或通过将计数添加到查询生成器中!

最新更新