简体   繁体   中英

How to get the query result for “not in between these two dates”

How to get the query result for "not in between these two dates"

Query1:

SELECT *
FROM   log
WHERE  employee_id = 2
AND    date BETWEEN '2013-12-01' and '2013-12-08'   

When I run the query I get the result

2013-12-1
2013-12-5

But how to get the result "all dates other than the above two". Or, how do I write the query to get the result below?

2013-12-2
2013-12-3
2013-12-4
2013-12-6
2013-12-7
2013-12-8

How about

 select * 
   from log 
  WHERE employee_id =2 
    AND NOT date between '2013-12-01' and '2013-12-08'

You could also do

 AND ( date < '2013-12-01'  OR date > '2013-12-08')

But, if your date column has timestamps that aren't midnight in it, you need to think harder about ranges. You'll need

AND ( date < '2013-12-01' AND date >= ('2013-12-08' + INTERVAL 1 DAY)

to get timesamps in the appropriate date range.

SELECT '2013-12-2'
UNION
SELECT '2013-12-3'
etc

There is a SQL standard operator, NOT BETWEEN which you can use:

select * 
from log 
WHERE employee_id = 2 and
      date not between '2013-12-01' and '2013-12-08';

This should be equivalent to:

select * 
from log 
WHERE employee_id = 2 and
      not (date between '2013-12-01' and '2013-12-08');

However, the latter is logically two operations (calculate the "date between" part and then negate the result). The first is one operation.

By the way, be very careful if the date is really stored as a datetime type. The time component can throw off equality operations and make the `between return unexpected results.

"select * from log WHERE employee_id =2 AND date between '2013-12-01' and '2013-12-08'"

the preceding query returns '2013-10-1' and '2013-12-5', and you are looking for

2013-12-2
2013-12-3
2013-12-4
2013-12-6
2013-12-7
2013-12-8

But your question is how to get the query result not in between the date . this makes sense?

Anyway According to your expected results. I think you are looking for

select * from log WHERE employee_id =2 AND  date != '2013-12-01' and date != '2013-12-08'

or

SELECT * FROM another_tab WHERE date NOT IN (
    select date from log WHERE employee_id =2 AND  date BETWEEN '2013-12-01' and  '2013-12-08'
)

I presume you want all the dates when there was no logged activity by the specified user. Following solution is based on another answer: SHOW ALL Dates data between two dates; if no row exists for particular date then show zero in all columns

    ;with d(date) as (
      select CAST('20131201' AS datetime)
      union all
      select date+1
      from d
      where date < CAST('20131208' AS datetime)
      )
    select d.date CDate
    from d
    left join [Log] l
           on l.[Date] = d.date AND l.employee_id=2
    WHERE l.employee_id IS NULL
    order by d.date
    OPTION (MAXRECURSION 0)

Sorry, this is for SQL Server not MySQL, I did not see the MySQL tag... Anyway I think this is what you expect and there will be similar solution for MySQL.

EDIT

There seems to be no WITH clause in MySQL :-(. If you are sure you have every day some other log record (other users actions, system events etc.), then the following command could work for you:

SELECT DISTINCT date FROM log l1 
  WHERE l1.date BETWEEN '2013-12-01' and '2013-12-08' 
  AND NOT EXISTS(SELECT * FROM log l2 WHERE 
                  l2.date = l1.date AND  l2.employee_id=2)

I did not test it, because I have presently no MySQL installed.

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