简体   繁体   中英

Mysql query between 3 date ranges

I have a php script that allows a user to generate reports, they have the option of selecting 3 date ranges. ie

2012-01-01 to 2012-01-31
AND 
2011-01-01 to 2011-01-31
AND 
2010-01-01 to 2010-01-31

But when I do my query, and there are no records between a certain date range, the query returns no values.

Here is my query:

 SELECT DealerName WHERE InterviewDate >= "2012/01/01 " AND InterviewDate <= " 2012/03/31"
 AND InterviewDate >= "2012/07/01 " AND InterviewDate <= " 2012/09/31"

I need a query that will return data if it is found in 1 of the date ranges, regardless of whether there is no data in the subsequent date ranges.

The data is summarized into a table where it would be possible for a the same data to be on multiple dates.

for example:

dealerName 1    2012-01-01
dealerName 1    2012-02-31
dealerName 2    2012-03-01
dealerName 1    2012-06-01
dealerName 1    2012-06-31

So If I wanted to query the data between 2012-01-01 and 2012-02-31 AND 2012-06-01 AND 2012-06-31. I realise that 1 date range could encompass all of these ranges, but the user may want to exclude data between these two date ranges.

A interview date can not be between x and y AND still be between z and zz. You are missing a OR - Also, have a look at the BETWEEN operator

SELECT DealerName
FROM table
WHERE InterviewDate BETWEEN '2012-01-01' AND '2012-01-31'
    OR InterviewDate BETWEEN '2011-01-01' AND '2011-01-31'
    OR InterviewDate BETWEEN '2010-01-01' AND '2010-01-31'

edit, sorry, missed the table name :)

You need to use OR instead of AND

WHERE   (InterviewDate BETWEEN '2012-01-01' AND '2012-01-31')
        OR
        (InterviewDate BETWEEN '2011-01-01' AND '2011-01-31')
        OR
        (InterviewDate BETWEEN '2010-01-01' AND '2010-01-31')

UPDATE 1

i think you have the Relational Division problem,

SELECT name
FROM   table1
WHERE  DATE BETWEEN '2012-01-01' AND '2012-02-31'
       OR
       DATE BETWEEN '2012-06-01' AND '2012-06-31'
GROUP BY name
HAVING COUNT(*) = 2

为什么不使用OR代替AND?

SELECT DealerName WHERE ( InterviewDate >= "2012/01/01 " AND InterviewDate <= " 2012/03/31" )  OR ( InterviewDate >= "2012/07/01 " AND InterviewDate <= " 2012/09/31" )

Use OR instead of AND

SELECT DealerName FROM tbl
WHERE InterviewDate >= '2012/01/01' AND InterviewDate <= '2012/03/31'
 OR InterviewDate >= '2012/07/01' AND InterviewDate <= '2012/09/31'

Here is my take away :) since you are checking on the month of January for year 2010, 2011, 2012. We can use MONTH and YEAR functions.

SELECT DealerName
FROM yourtable
WHERE Month(InterviewDate) = 1
AND Year(InterviewDate) IN (2010, 2011, 2012)
;

SELECT DealerName
FROM yourtable
WHERE Month(InterviewDate) IN (1, 2) // if there are particular months
AND Year(InterviewDate) IN (2010, 2011, 2012)
;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM