Mysql - 使用多个连接表查找丢失的日期
Posted
技术标签:
【中文标题】Mysql - 使用多个连接表查找丢失的日期【英文标题】:Mysql - Finding missing dates with multiple join tables 【发布时间】:2014-02-27 19:16:12 【问题描述】:我需要一些帮助来编写一个 mysql 查询。我需要查找特定业务 ID/项目 ID 的给定日期范围内所有错过的报告。
基本上,对于给定的企业 ID,我需要知道项目的名称以及报告缺失或未标记为已完成的所有日期。
我正在使用日历表技巧(如 here 和 here 所述)来查找缺少的报告日期,但我在加入项目表以查找缺少报告的相关项目/业务时遇到问题.
我基本上需要一个结果集,它会给我类似这样的数据:
+------------+-----------+--------------+
| project_id | name | missing_date |
+------------+-----------+--------------+
| 1 | Project 1 | 2014-01-01 |
| 1 | Project 1 | 2014-01-03 |
| 1 | Project 1 | 2014-01-04 |
| 1 | Project 1 | 2014-01-07 |
| 1 | Project 1 | 2014-01-09 |
| 2 | Project 2 | 2014-01-02 |
| 2 | Project 2 | 2014-01-03 |
| 2 | Project 2 | 2014-01-04 |
+------------+-----------+--------------+
这是我的架构:
projects table:
+----------------+------------------+------+-----+-------------------+----------------+
| Field | Type | Null | Key | Default | Extra |
+----------------+------------------+------+-----+-------------------+----------------+
| project_id | int(10) unsigned | NO | PRI | NULL | auto_increment |
| business_id | int(10) unsigned | NO | MUL | NULL | |
| name | tinytext | YES | | NULL | |
+----------------+------------------+------+-----+-------------------+----------------+
reports table:
+---------------------+------------------+------+-----+-------------------+----------------+
| Field | Type | Null | Key | Default | Extra |
+---------------------+------------------+------+-----+-------------------+----------------+
| report_id | int(10) unsigned | NO | PRI | NULL | auto_increment |
| project_id | int(10) unsigned | NO | MUL | NULL | |
| report_date | date | NO | MUL | NULL | |
| completed | bit(1) | NO | | b'0' | |
+---------------------+------------------+------+-----+-------------------+----------------+
calendar table:
+--------------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+--------------+-------------+------+-----+---------+-------+
| dt | date | NO | PRI | NULL | |
| month_name | varchar(9) | YES | | NULL | |
| day_name | varchar(9) | YES | | NULL | |
| y | smallint(6) | YES | | NULL | |
| q | tinyint(4) | YES | | NULL | |
| m | tinyint(4) | YES | | NULL | |
| d | tinyint(4) | YES | | NULL | |
| dw | tinyint(4) | YES | | NULL | |
| w | tinyint(4) | YES | | NULL | |
| is_weekday | bit(1) | YES | | NULL | |
| is_holiday | bit(1) | YES | | NULL | |
| holiday_desc | varchar(32) | YES | | NULL | |
+--------------+-------------+------+-----+---------+-------+
下面的查询可以返回未完成报告的列表,但我仍然需要用根本没有报告记录的日期来填补空白。
select
p.project_id,
p.name,
c.dt as missing_date,
r.completed
from reports r
join projects p on (r.project_id = p.project_id)
right join calendar c on (c.dt = r.report_date)
where c.dt >= '2014-02-01'
and c.dt <= '2014-02-10'
-- and r.report_date is null /** THE RESULT SET IS EMPTY IF I UNCOMMENT THIS **/
and r.completed = false
and c.is_holiday = false
and c.is_weekday = true
and p.business_id = 1001
order by p.project_id, r.report_date, c.dt;
任何帮助将不胜感激!
【问题讨论】:
您的查询结果是什么?为什么要对日历表进行右连接? 我得到一个空的结果集。我需要加入日历表,以便确定应该创建报告的所有日期。 我看不出需要 right 连接,为什么要包含所有 calendar 记录而不匹配 reports记录? 您可以尝试拆分查询并检查子查询的结果:A:“日历没有连接,只有日期”,B:“日历没有连接和完整的where语句”,C:“报告加入日历”,... 【参考方案1】:我阅读了您提供的链接,这种存储日期的方法非常棒。 您在查询中使用的任何方式:
right join calendar c on (c.dt = r.report_date)
但我在您的报告表中没有看到任何 report_date。 如果问题是因为它,我建议你检查一下,因为除此之外你的查询似乎工作正常。
【讨论】:
抱歉,报告表确实包含 report_date 列。我在发布示例模式时不小心删除了它。我已经编辑了示例架构以显示它现在在那里。【参考方案2】:好的,我终于让它工作了。这是一个相当复杂的查询,需要在项目表上进行内部连接,但我不知道更好的方法 - 我是一个 Java 人,这些天过于依赖休眠方式来构建我的查询:)。如果有人有更有效的解决方案,我会全力以赴!
最终查询:
select
p.project_id,
p.name,
c.dt as missing_date,
r.report_date,
r.completed
from calendar c
inner join (
select
p1.project_id,
p1.name
from projects p1
where p1.project_id = 1005
-- where p1.business_id = 1001 /** OR USE THE BUSINESS ID **/
) p on c.dt between '2014-02-01' and '2014-02-28'
left join reports r on r.report_date = c.dt
and r.project_id = p.project_id
and r.completed = false
where (r.report_date is null or r.completed = false)
and c.is_holiday = false
and c.is_weekday = true
order by p.project_id, c.dt;
产生正确的结果:
+------------+--------------+--------------+-------------+-----------+
| project_id | name | missing_date | report_date | completed |
+------------+--------------+--------------+-------------+-----------+
| 1005 | Project 1005 | 2014-02-03 | 2014-02-03 | 0 |
| 1005 | Project 1005 | 2014-02-04 | 2014-02-04 | 0 |
| 1005 | Project 1005 | 2014-02-05 | NULL | NULL |
| 1005 | Project 1005 | 2014-02-06 | 2014-02-06 | 0 |
| 1005 | Project 1005 | 2014-02-07 | NULL | NULL |
| 1005 | Project 1005 | 2014-02-10 | 2014-02-10 | 0 |
| 1005 | Project 1005 | 2014-02-11 | 2014-02-11 | 0 |
| 1005 | Project 1005 | 2014-02-12 | 2014-02-12 | 0 |
| 1005 | Project 1005 | 2014-02-13 | NULL | NULL |
| 1005 | Project 1005 | 2014-02-14 | NULL | NULL |
| 1005 | Project 1005 | 2014-02-18 | NULL | NULL |
| 1005 | Project 1005 | 2014-02-19 | NULL | NULL |
| 1005 | Project 1005 | 2014-02-20 | 2014-02-20 | 0 |
| 1005 | Project 1005 | 2014-02-21 | 2014-02-21 | 0 |
| 1005 | Project 1005 | 2014-02-24 | 2014-02-24 | 0 |
| 1005 | Project 1005 | 2014-02-25 | 2014-02-25 | 0 |
| 1005 | Project 1005 | 2014-02-26 | NULL | NULL |
| 1005 | Project 1005 | 2014-02-27 | NULL | NULL |
| 1005 | Project 1005 | 2014-02-28 | NULL | NULL |
+------------+--------------+--------------+-------------+-----------+
感谢各位朋友的帮助!
【讨论】:
以上是关于Mysql - 使用多个连接表查找丢失的日期的主要内容,如果未能解决你的问题,请参考以下文章
MySQL 无法在 FROM 多个表连接中指定要更新的目标表
DBeaver 错误 2013:与 MySQL 的连接丢失。为啥?