Aditya P Bhatt
Aditya P Bhatt

Reputation: 22081

CakePHP Find Query SUM() two fields from two different tables

I am using CakePHP Find query to get SUM() from two diff fields in two diff db tables:

$total = $this->Model->find('all',array('fields' =>
array(
  'SUM(Model1.amount * Model2.purchase * Model3.rental * Model4.manu) as total'),
  'group' => array( 'Model.trackby_id' ) )
);

I have found sum() method useful for same tables two diff fields, but it does not seems working when two diff fields from diff tables.

Anybody have a solution for this?

Upvotes: 0

Views: 9117

Answers (1)

Moz Morris
Moz Morris

Reputation: 6771

You need to specify your SQL fragment in the fields array in the parameters of the find method.

$total = $this->Model->find('all', array(
  'fields' => array(
    'SUM(Model.price + OtherModel.price) AS total'
  ),
  'group' => 'Model.id'
));

Upvotes: 3

Related Questions