用除法建立的查询

时间:2019-07-11 17:44:03

标签: mysql cakephp-3.0

我需要每48小时从datetime字段发送一封电子邮件。 我的SQL查询是:

select * from leads where datediff(NOW(),`level7_email_sent`)%2 = 0

我如何使用CakePHP查询生成器?我的问题是分裂。

我所做的是:

        $leads = $this->Leads->find();
            //->contain([])
        $leads->where(function (QueryExpression $exp, Query $q) {
                $datediff = $q->func()->dateDiff([
                    'NOW()' => 'literal',
                    'level7_email_sent' => 'identifier'
                ]);
                return $exp->eq('engagement_level',7)->eq($datediff,7);
            });

谢谢,我不知道该怎么做。

1 个答案:

答案 0 :(得分:0)

我使用HAVING解决了

    $leads = $this->Leads->find();
    //->contain([])
    $datediff = $leads->func()->dateDiff([
        'NOW()' => 'literal',
        'level8_email_sent' => 'identifier'
    ]);
    $leads->select(['dateDiff'=>$datediff])
        ->enableAutoFields(true)
        ->where(['engagement_level'=>7])
        ->having(['MOD(`dateDiff`,2)'=>0]);