1

CakePHP 1.3 でこのクエリを作成する方法

 SELECT "Redeem_log"."benefit_id", count("Redeem_log"."benefit_id") as JUMLAH FROM  "redeem_logs" "Redeem_log" 
LEFT JOIN "benefits" "Benefit" ON ("Redeem_log"."benefit_id" = "Benefit"."id") 
LEFT JOIN "merchants" "Merchant" ON ("Benefit"."merchant_id" = "Merchant"."id")  
LEFT JOIN "merchant_types" "Merchant_type" ON ("Merchant"."merchant_type_id" = "Merchant_type"."id") 
WHERE "Redeem_log"."benefit_id" IS NOT NULL AND ("Merchant_type"."merchant_type"='lokal' OR "Merchant_type"."merchant_type"='nasional') GROUP BY "Redeem_log"."benefit_id"  ORDER BY "JUMLAH" DESC  


私はbegsTO、hasMany、またはectを使用したくありません

var $belongsTo = array(
    'Benefit' => array('className' => 'Benefit',     'foreignKey' => 'benefit_id'),
    'Merchant' => array('className' => 'Merchant', 'foreignKey' => 'merchant_id')
);

左結合は次のようなものです:

SELECT "Redeem_log"."benefit_id", count("Redeem_log"."benefit_id") as JUMLAH 
FROM "redeem_logs" "Redeem_log" 
LEFT JOIN "benefits" "Benefit" ON ("Redeem_log"."benefit_id" = "Benefit"."id") 
LEFT JOIN "merchants" "Merchant" ON ("Redeem_log"."merchant_id" = "Merchant"."id") 
WHERE "Redeem_log"."benefit_id" IS NOT NULL  GROUP BY "Redeem_log"."benefit_id"  
ORDER BY "JUMLAH" DESC  
4

2 に答える 2

0

クエリのパラメーターは少し怖いように見えますが、詳細を見ると理にかなっています。

$params = array(
    'recursive' => -1,
    'fields' => array('RedeemLog.benefit_id', 'COUNT(RedeemLog.benefit_id) as JUMLAH'),
    'conditions' => array(
        array('NOT' => array('RedeemLog.benefit_id' => null)),
        array('Merchanttype.merchant_type' => array('lokal', 'nasional')),
     ),
     'joins' => array(
         array(
             'table' => 'benefits',
             'alias' => 'Benefit',
             'type' => 'LEFT',
             'conditions' => array('RedeemLog.benefit_id = Benefit.id')
         ),
         array(
            'table' => 'merchants',
            'alias' => 'Merchant',
            'type' => 'LEFT',
            'conditions' => array('Benefit.merchant_id = Merchant.id')
         ),
         array(
            'table' => 'merchant_types',
            'alias' => 'Merchanttype',
            'type' => 'LEFT',
            'conditions' => array('Merchant.merchant_type_id = Merchanttype.id')
         ),
     ),
     'order' => 'JUMLAH DESC',
     'group' => 'RedeemLog.benefit_id',
 );

そして最後に見つけたケーキ

 $result = $this->RedeemLog->find('all', $params);

それがあなたのために働くことを願っています。

于 2013-03-07T15:01:03.397 に答える