我想选择计数" id"和cakephp-3.x中的价格总和 简单的mysql查询: -
SELECT COUNT(id) as count, SUM(price) as total_price FROM bookings WHERE id=3;
我不知道如何在cakephp-3.x
中编写此查询请告诉我如何在cakephp-3.x
中编写此查询答案 0 :(得分:2)
首先尝试阅读如何使用ORM的查询构建器:
Query Builder - Aggregates Group and Having
这将是这些方面的内容
$query = $bookings->find();
$query->select([
'count' => $query->func()->count('id'),
'total_price' => $query->func()->sum('price')
])->where(['id' => 3]);
计数将来自结果数量或将计数添加到查询构建器中!