mysql - 如何检查给定日期是否至少包括为用户列出的每个文档组?

标签 mysql sql date between

我想开发一个 SQL 查询来检查给定日期是否至少在每个文档组中。 下表

DocID    UserID     StartDAte     EndDAte     OfficialName
1        1         10/1/18       10/3/18       A
2        1         10/5/18       10/10/18      A
3        1         10/1/18       10/9/18       B
4        1         10/1/18       10/9/18       C
5        1         10/1/18       10/5/18       D
6        1         10/7/18       10/20/18      D

有 4 个文档组,即 A、B、C、D。需要检查给定日期是否至少在每个组中的每个文档中。

eg date : 10/2/18 is in first record of A,B,C, and first record of D. So it is passed.
eg date : 10/4/18 is not in either of documents in A hence failed.
eg date : 10/8/18 is second document in A,B,C, and second document in D hence passed.
eg date : 10/6/18 is in A but not in D hence failed.

因为我必须为给定的用户和日期写这个,所以我必须为“OfficialName”使用“IN”子句,但是我怎么能添加“OR”来检查每个“OfficialName”中的任何文件中的日期为给定用户的所有文档分组?

感谢任何帮助。 需要补充一些不清楚的地方。正式名称中的文件数量不固定。它可以是一个或多个。

最佳答案

聚合并获取组的不同计数。如果你得到 4,则你有一场比赛,否则你没有。

SELECT count(DISTINCT t.officialname)
       FROM elbat t
       WHERE t.userid = <given user>
             AND t.startdate <= <given date>
             AND t.enddate >= <given date>;

您还可以添加 HAVING count(DISTINCT t.officialname) = 4 以在且仅当没有匹配项时获得空集。

关于mysql - 如何检查给定日期是否至少包括为用户列出的每个文档组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53924648/

相关文章:

mysql - 更新两个表 - 错误 - 'product_varities.product_varities_id' 中的未知列 'where clause'

php - 提交日期范围之间的日期 - mySQL

php - MySQL 左连接 : Limit parent table

sql - sqlite3命令行返回码不一致

database - 如何为财政年度创建数据库记录

日期时间到日期的 Mysql 转换未反射(reflect)在输出中

MySQL:在删除触发器中引用当前行

java - MySQL语法错误异常 : Unknown column xxx in 'field list'

mysql - 如何在 MySQL 中返回数据透视表输出?

Python:使用日历将日期编号转换为日期名称