mahipal negi
mahipal negi

Reputation: 317

Cake 3x:- how to use count and sum in find query

i want to select count "id" and sum of price in cakephp-3.x simple mysql query:-

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

i don't know how to write this query in cakephp-3.x

Please let me know how to write this query in cakephp-3.x

Upvotes: 1

Views: 1699

Answers (1)

the1dv
the1dv

Reputation: 931

First try reading up on how to use the query builder of the ORM:

Query Builder - Aggregates Group and Having

It will be something along these lines

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

The count will come from the number of results or by adding in the count to the query builder!

Upvotes: 2

Related Questions