【问题标题】:Mysql- Get dates between from date and to dateMysql-获取从日期到日期之间的日期
【发布时间】:2017-11-16 03:59:25
【问题描述】:

我有一个表tbl_patient:包含from_date, to_date, patient_namegender

from_date    to_date      patient_name   gender

2017-06-10   2017-06-13   AAA            Male
2017-06-08   2017-06-11   BBB            Female
2017-06-13   2017-06-15   CCC            Male

我必须从 tbl_patient 创建一个新表 tbl_details,其中包含日期(在 from_dateto_date 之间,来自 tbl_patient),total_number of patients in当天男数女数

date          patients    Male    Female

2017-06-10    2           1       1
2017-06-11    2           1       1
2017-06-12    1           1       0
2017-06-13    2           2       0
2017-06-08    1           0       1
2017-06-09    1           0       1    
2017-06-14    1           1       0
2017-06-15    1           1       0

我的问题是我无法编写查询来查找 from_date 和 to_date 之间的日期

I tried 

SELECT DATE_ADD( MIN(from_date), interval @num := @num+1 day) AS date_sequence, 
tbl_patient.* FROM tbl_patient
HAVING DATE_ADD(MIN(from_date), interval @num day) <= MAX(to_date)

但是没有运气..

请有人可以帮助我..

【问题讨论】:

  • 预期输出中患者和日期之间的逻辑是什么?为什么10-06、11-6有2个病人,12-06只有1个?
  • 您是否正在寻找一个查询,该查询可让您在从和到之间每天按男性/女性划分患者数?
  • 你能在这里回显你当前的查询吗
  • 最简单的做法可能是创建一个日历表,然后在范围匹配上将其内部连接到当前表。
  • 考虑处理应用代码中数据显示的问题

标签: mysql


【解决方案1】:

表格:

create table tbl_patient (
    from_date date, 
    to_date date,
    patient_name varchar(45),
    gender varchar(45)
)

插入值:

insert into tbl_patient values ('2017-06-10','2017-06-13','AAA','Male');
insert into tbl_patient values ('2017-06-08','2017-06-11','BBB','Female');
insert into tbl_patient values ('2017-06-13','2017-06-15','CCC','Male');

查询:

 set @mindate = (select min(from_date) - interval 1 day as mindate from tbl_patient);
set @maxdate = (select max(to_date) as mindate from tbl_patient);
select date_value, count(patient_name), count(case when gender = 'male' then 1 end) male, 
count(case when gender = 'female' then 1 end) female 

from (select @startDate := @startDate + Interval 1 day as date_value
from (select 0 union all select 1 union all select 3 union all select 4 
    union all select 5 union all select 6 union all select 6 union all select 7 
    union all select 8 union all select 9) t,
(select 0 union all select 1 union all select 3 
    union all select 4 union all select 5 union all select 6 
    union all select 6 union all select 7 union all select 8 union all select 9) t2,
(select 0 union all select 1 union all select 3 union all select 4 union all select 5 union all select 6 union all select 6 union all select 7 union all select 8 union all select 9) t3, 
(select 0 union all select 1 union all select 3 union all select 4 union all select 5 union all select 6 union all select 6 union all select 7 union all select 8 union all select 9) t4,
(SELECT @startDate := @mindate ) t5 
where @startDate < @maxdate ) generateCalendar

left join tbl_patient on date_value between from_date and to_date
group by date_value;

您也可以使用查询生成日历。只需找到最小和最大日期。

注意:最短日期应少 1 天。

【讨论】:

  • @next2u 更新了答案。让我知道它是否有用。
  • 什么是generateCalendar?这个怎么用?
  • @next2u GenerateCalender 只不过是生成包含开始日期和结束日期之间所有日期的行。您是否使用数据执行了上述查询?
  • @next2u 你能告诉我 tbl_patients 中所有列的数据类型是什么。
  • from_date 和 to_date 是日期,name 是 varchar,gender 是 enum('Male', 'Female')
猜你喜欢
  • 1970-01-01
  • 2018-01-11
  • 2013-04-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-01-09
  • 1970-01-01
  • 2010-09-21
相关资源
最近更新 更多