在mysql数据库中,我有这样的表:
StartDate EndDate Price
01.01.2017 01.06.2017 100
02.06.2017 01.12.2017 150在几天之间获得价格的最佳解决方案是什么?例如,如果我发送日期25.05.2017 as start date和05.06.2017 as end date,我需要得到这两个日期之间的价格总和。
发布于 2018-03-29 16:07:22
尝尝这个
select sum(Price) as total_price from table_name
where StartDate >= 'given_startdate' and EndDate <= 'given_enddate'发布于 2018-03-29 16:19:43
要查找重叠的期间,请使用以下逻辑:
where start_1 <= end_2 and end_1 >= start_2在您的案例中:
where startdate <= date '2017-06-05' and enddate >= date '2017-05-25'编辑:
看起来您想要将日期范围扩展到每天一行。加入一个日历表格(如果你没有日历表格,请创建它,你会在很多地方使用它):
select sum(price)
from mytable join calendar
on calendardate between startdate and enddatehttps://stackoverflow.com/questions/49550935
复制相似问题