Cakephp sum() 与乘法

Cakephp sum() with multiplication

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

使用 CakePhp 2.6

我想在此处查找第一个 table 的数据..

$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 金额匹配,则 incentive 将为总金额*price1,与 target2 和 target3 相同。我在哪里使用租户操作员。目标价格应与相同的查找条件数据相乘。

我搜索 google 和堆栈溢出但找不到解决方案。但是,我很感谢堆栈溢出和所有的家伙,我能够找到很多技巧和解决方案。

虽然你可以按照我的想法尝试下面的解决方案,但我还没有得到确切的答案,

您需要在 MYSQL、

中使用 WHEN 关闭
$incenve = 'CASE WHEN amount>5000 THEN "FIRST Target" WHEN amount>3000 THEN "Second Target" ELSE 0 '

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

'incentive' => $incentive 

谢谢Oldskool for this solution 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 来获取激励值了。