Cakephp sum()乘以

时间:2016-11-17 06:56:40

标签: if-statement foreach cakephp-2.0 ternary-operator

是否可以找到带有附加条件的cakephp,或者我们可以将sum()字段与条件变量相乘? 我不知道,让你们知道我在问什么可能是个最好的问题。所以我提供了有关问题的更多细节。我能够通过一些技巧找到具有单个阵列的类似数据。但我无法找到这个。请帮助我或给我一个想法来完成这样的任务。感谢。

使用CakePhp 2.6

我想在这里找到第一张表中的数据..

$incentiveweekly=$this->Incentive->find('all',
            array('conditions' => 
                array( 
                    "AND"=>array(
                        "Incentive.fromdate >=" => $from,
                        "Incentive.todate <=" => $to
                    )
                )
            )
    );

    according to number of rows. I have to find another table
Here is the result of above find condition.
                Array
            (
                [Incentive] => Array
                    (
                        [id] => 2
                        [target1] => 3000
                        [price1] => 1.5
                        [target2] => 6000
                        [price2] => 2.5
                        [target3] => 8000
                        [price3] => 3.5
                        [formonth] => 
                        [type] => 1
                        [fromdate] => 2016-11-13
                        [todate] => 2016-11-21
                        [updatedby] => 1
                        [created] => 2016-11-15 23:57:21
                        [modified] => 2016-11-15 23:57:21
                    )

            )
            Array
            (
                [Incentive] => Array
                    (
                        [id] => 3
                        [target1] => 3000
                        [price1] => 1.5
                        [target2] => 6000
                        [price2] => 2.5
                        [target3] => 8000
                        [price3] => 3.5
                        [formonth] => 
                        [type] => 1
                        [fromdate] => 2016-11-24
                        [todate] => 2016-11-28
                        [updatedby] => 1
                        [created] => 2016-11-15 23:57:21
                        [modified] => 2016-11-15 23:57:21
                    )

            )

现在我想根据数组记录的数量找到数组。

    $byweek=array();    // Storing Customer data by Target dates in array()             
    foreach ($incentiveweekly as $weekly){ 
        print_r($weekly);
        $target3=$weekly['Incentive']['target3'];
        $target2=$weekly['Incentive']['target2'];
        $target1=$weekly['Incentive']['target1'];
        $price3=$weekly['Incentive']['price3'];
        $price2=$weekly['Incentive']['price2'];
        $price1=$weekly['Incentive']['price1'];
        $byweek[]=$customers=$this->Customer->find('all',array(
            'fields'=>array(
            'SUM(amount) AS amount',
            'created_by'
            ),
            'group' => 'Customer.created_by',
            'conditions' => array(
                            "AND" =>array(
                                "Customer.created >=" => $weekly['Incentive']['fromdate'], 
                                "Customer.created <=" => $weekly['Incentive']['todate']
                                )
                            ),
            'recursive'=>-1     )
                    );
        //print_r($byweek);
        }

我得到的结果就像......

            Array
            (
                [0] => Array
                    (
                        [0] => Array
                            (
                                [Customer] => Array
                                    (
                                        [created_by] => 3
                                    )

                            )

                    )

            )
            Array
            (
                [0] => Array
                    (
                        [0] => Array
                            (
                                [Customer] => Array
                                    (
                                        [created_by] => 3
                                    )

                            )

                    )

                [1] => Array
                    (
                        [0] => Array
                            (
                                [Customer] => Array
                                    (
                                        [created_by] => 1
                                    )

                            )

                        [1] => Array
                            (
                                [Customer] => Array
                                    (
                                        [created_by] => 2
                                    )

                            )

                    )

            )

但是我希望在我使用三元运算符的if else条件下,该数量会增加。

    $value[0]['amount']>=$valuem['Incentive']['target3']?"Target Third":($value[0]['amount']>=$valuem['Incentive']['target2']?"Target Second":($value[0]['amount']>=$valuem['Incentive']['target1']?"Target First":"None"))

查找详细信息的主要目的是。我想创建一个激励金额,其中总销售额应该与给定的目标金额相匹配。如果target1金额与一个匹配,那么激励将是总金额* price1并且与target2和target3相同。我在哪里使用租赁运营商。目标价格应与相同的查找条件数据相乘(按条件)。

我搜索谷歌和堆栈溢出但无法找到它的解决方案。但是,我感谢堆栈溢出和你所有的gusy,我能够找到很多技巧和解决方案。

2 个答案:

答案 0 :(得分:0)

我还没有完全按照我的想法尝试使用以下解决方案,

你需要在MYSQL中使用WHEN,

$incenve = 'CASE WHEN amount>5000 THEN "FIRST Target" WHEN amount>3000 THEN "Second Target" ELSE 0 '

以及上述查询中的内部字段

'incentive' => $incentive 

答案 1 :(得分:0)

感谢Oldskool了解此解决方案Virtualfields with If Condition

最终我,通过此代码获得我的解决方案。我按预期得到了结果。 :)

            $this->Customer->virtualFields=array(
                  'incentive' => "if(SUM(Customer.amount)>=$target3,(SUM(Customer.amount))*$price3,if(SUM(Customer.amount)>=$target2,(SUM(Customer.amount))*$price2,if(SUM(Customer.amount)>=$target1,(SUM(Customer.amount))*$price1,0)))",
            );

现在我可以使用Virtualfied获取奖励值。