简体   繁体   English

如何获取“不在这两个日期之间”的查询结果

[英]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: 查询1:

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. 但是,如果您的date列的时间戳不是午夜时间,则需要更加思考范围。 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: 有一个SQL标准运算符, NOT BETWEEN ,您可以使用:

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. 顺便说一句,如果date确实存储为datetime类型,请务必小心。 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'" “select * from log WHERE employee_id = 2 AND date between between 2013-12-01'和'2013-12-08'”

the preceding query returns '2013-10-1' and '2013-12-5', and you are looking for 前面的查询返回'2013-10-1'和'2013-12-5',您正在寻找

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 . 但是你的问题是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. 对不起,这是针对SQL Server而不是MySQL,我没有看到MySQL标签......无论如何,我认为这是你所期望的,并且会有类似的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: MySQL中似乎没有WITH子句:-(。如果您确定每天都有其他日志记录(其他用户操作,系统事件等),那么以下命令可能对您有用:

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. 我没有测试它,因为我目前没有安装MySQL。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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