Cakephp使用单个查询获得所有月份记录,即使用单个查找

时间:2015-08-12 06:26:23

标签: php mysql cakephp-2.0

我正在使用平台cakephp进行电子商务,并使用谷歌图表进行报告。我的要求是按照所有12个月获取所有记录,因此我使用了以下代码一个月

查询

    gama    theta   detectionsLimit NSMOOTH NREF    NOBS    sma lma PosTrades   NegTrades   Acc AvgWin  AvgLoss     Return
10.0    1.00    3            10     50  10  15  33  11  2       0.846154    0.0451529   -0.019449800    1.54942
1.0 1.00    5            5       80 40  15  33  7   2       0.777778    0.0676022   -0.008395400    1.54916
1.0 1.00    1           20       80 40  15  33  6   1       0.857143    0.0673241   -0.017465300    1.44823

代码

SELECT COUNT(*) AS count FROM orderproductmasters AS Orderproductmaster 
    LEFT JOIN ordermasters AS Ordermaster ON 
        (Orderproductmaster.ordermaster_id = Ordermaster.id) LEFT JOIN productmasters AS Productmaster ON 
        (Orderproductmaster.productmaster_id = Productmaster.id) 
        WHERE Ordermaster.orderstatusmaster_id = 1 AND Month(Orderproductmaster.created) = 8

因为,我需要按照Jan,feb,march和所有12个月的记录...,所以在12个月内我使用以下代码

 $this->Orderproductmaster->find('count',
        array('conditions'=>array('Ordermaster.orderstatusmaster_id'=>1,'
            Month(Orderproductmaster.created)'=>8)));

所以问题可能很愚蠢,但是有可能在不使用for循环的情况下获得所有月份记录,即在单个查询中。 提前致谢

3 个答案:

答案 0 :(得分:0)

 $options = array();
    $options['fields'] = array('COUNT(Orderproductmaster.id)'); 
    $options['conditions'] = array('Ordermaster.orderstatusmaster_id = 1',
    'Month(Orderproductmaster.created) BETWEEN 1 AND 12');
    $options['joins'] = array(
                            array(
                            'table' => 'ordermasters',
                            'alias' => 'Ordermaster',
                            'type' => 'left',
                            'conditions' => array(
                                 'Orderproductmaster.ordermaster_id = Ordermaster.id'
                             )
                          ),
                        array(
                            'table' => 'productmasters',
                            'alias' => 'Productmaster',
                            'type' => 'left',
                            'conditions' => array(
                            'Orderproductmaster.productmaster_id = Productmaster.id'                
                           )
                         )
                                                        );
            $options['group'] => array('Month(Orderproductmaster.created)');    
  $this->Orderproductmaster->find('all',$options);

答案 1 :(得分:0)

我认为,您可以通过在存储过程中使用游标来满足您的需求。然后使用存储过程来cake-php。

数据库端的示例是here

答案 2 :(得分:0)

如下所示:

 $this->Orderproductmaster->find('count',
        array(
     'fields'=>'DISTINCT(Month(Orderproductmaster.created)),
     'conditions'=>array('Ordermaster.orderstatusmaster_id'=>1,'
            Month(Orderproductmaster.created)'=>8)));