0

how can I convert this query to use cakePHP's method find() ?

query:

SELECT Ch.id, COUNT( DU.d_id ) AS a, SUM(DU.d_id) AS b
FROM c AS Ch
LEFT JOIN du AS DU ON DU.c_id = Ch.id AND MONTH( DU.created ) = MONTH( NOW( ) )
LEFT JOIN d AS D ON DU.d_id = D.id
GROUP BY Ch.id

1 Answer 1

1
$Ch->find(
    'all',      
    'fields' => array(
               'Ch.id','COUNT( DU.d_id ) AS a', 'SUM(DU.d_id) AS b'),
    'joins'  => array(
                      array(
                            'table' => 'du',
                            'alias' => 'DU',
                            'type' => 'LEFT',
                            'conditions' => array('DU.c_id = Ch.id AND MONTH( DU.created ) = MONTH( NOW( )')),
                       array(
                            'table' => 'd',
                            'alias' => 'D',
                            'type' => 'LEFT',
                            'conditions' => array('DU.c_id = Ch.id AND MONTH( DU.created ) = MONTH( NOW( )')),
    'group' => 'Ch.id');          

I haven't tested. More info on Cake JOINs:

http://book.cakephp.org/view/1018/find

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.