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

Wednesday, February 16, 2022

[FIXED] MySQL query - 2 sets of conditions

 February 16, 2022     cakephp, logical-operators, mysql     No comments   

Issue

I've been thinking about this for too long, and have got myself confused! I'm trying to come up with a MySQL query in CakePHP, for the following scenario:

Each record has 2 dates; for the sake of simplicity I'll call them F & I. These determine the quality of the data, as follows:

  • Bad data - F & I are both null
  • Good data - F & I are both non-null and represent dates within the past 3 months 'In between' data - everything else

The first 2 are easy enough:

Red:

$conditions['AND'] = array('F' => null, 'I' => null);

Green:

$conditions['AND'] = array(
     'F >=' => Three months ago,
     'I >=' => Three months ago
)

But I'm having trouble with the third option. I've tried a few things, and I know it's something like:

$conditions['NOT'] = array(
     'AND' => array(
         'F >=' => Three months ago,
         'I >=' => Three months ago
     ),
     'AND' => array(
          'F' => null,
          'I' => null
     )
)

but obviously that's not going to work because it has two different values for 'AND'.


Solution

I think there's a problem in your boolean logic. If you want F & I to represent dates within the past three months, then you're ruling out any entries where F >= three months OR I >= three months.

It'd be less confusing to use De Morgan’s law on your boolean logic -- NOT (F >= three months OR I >= three months) is the same as F < three months AND I < three months. Therefore, for the third rule, your conditions array would look like:

array('conditions' => array('AND' => array('F <' => three months, 'I <' => three months)));



Answered By - Kai
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

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