PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Wednesday, January 5, 2022

[FIXED] Query builded with division

 January 05, 2022     cakephp-3.0, mysql     No comments   

Issue

I need to send an email every 48 hours from a datetime field. My sql query is:

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

How I do it with CakePHP query builder? My problem is division.

Whats I done is:

        $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);
            });

I don't know how to do the division, thanks.


Solution

I solved using 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]);


Answered By - Igor
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home
View mobile version

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing