[thelist] SQL query question about AND OR and date ranges

Jay Blanchard jay.blanchard at niicommunications.com
Thu Sep 5 09:31:01 CDT 2002


Howdy there brain trust!

I have a query;

mysql> SELECT cdr.discn_dt, COUNT(cdr.ani) AS quantity, SUM(cdr.calldur/60)
AS minutes
    -> FROM tblCDR cdr LEFT OUTER JOIN tblCDRExempt e
    -> ON cdr.ani = e.ExemptCDR
    -> WHERE e.ExemptCDR IS NULL
    -> AND cdr.discn_dt BETWEEN '2002-07-01' AND '2002-07-30'
    -> AND cdr.dialedno LIKE '800%'
    -> GROUP BY cdr.discn_dt;
Which works properly, returning the proper range of dates. When I add an OR
clause

mysql> SELECT cdr.discn_dt, COUNT(cdr.ani) AS quantity, SUM(cdr.calldur/60)
AS minutes
    -> FROM tblCDR cdr LEFT OUTER JOIN tblCDRExempt e
    -> ON cdr.ani = e.ExemptCDR
    -> WHERE e.ExemptCDR IS NULL
    -> AND cdr.discn_dt BETWEEN '2002-07-01' AND '2002-07-30'
    -> AND cdr.dialedno LIKE '800%'
    -> OR cdr.dialedno LIKE '866%'
    -> GROUP BY cdr.discn_dt;
It returns dates from outside of the range specified. I do not understand
why it does this and cannot find anything on the web or in a book that
explains the behavior close enough for me to discern. Since I cannot find an
explanation I cannot find a proper solution either. Has anyone seen this
behavior or would anyone have a solution?

Thanks muchly!

Jay

*****************************************************
* Texas PHP Developers Conf  Spring 2003            *
* T Bar M Resort & Conference Center                *
* New Braunfels, Texas                              *
* Contact jay.blanchard at niicommunications.com       *
*                                                   *
* Want to present a paper or workshop? Contact now! *
*****************************************************





More information about the thelist mailing list