MySQL日期之间的范围:第一个订单+ 6个月

时间:2015-03-01 00:33:45

标签: mysql date

我正在使用MySQL Workbench来运行我的查询。

我想运行几个不同的日期查询,我不知道它是否可以在SQL中使用。

1)将报告从系统中的第一个日期运行到X日期 作为计算的一部分,我如何找到第一个订单ID或日期? 我知道如果我有开始日期,我可以使用:
where T5.date_purchased BETWEEN '2005-01-01' AND '2015-12-31'或者: where T5.date_purchased BETWEEN '2005-01-01' AND CURDATE() + INTERVAL 1 DAY

更新
-----查询2已被回答(尽管有任何改进)-----

2)从产品第一次出现时运行报告,到+ 6个月(查看订单的前6个月)即:小工具1(第一个订单)+从订单日期开始的6个月。类似的东西:
where widget=widgetID AND date between widget1's first purchase and +6 months

更新:这不起作用,但这有点像我在想的那样:
where (T3.products_id = 39) and DATE_ADD((T1.products_date_added), INTERVAL 1 MONTH)

我会使用我的P3.products_date_added,但是,我不知道如何正确使用它作为上述的一部分。

这些都是可能的,我知道如何在我知道日期时提取记录,我只是不知道是否可以使用'日期未知'或者我必须运行“预报告”第一。或者它是excel中的后期处理过滤器?

提前谢谢。

2 个答案:

答案 0 :(得分:1)

回答问题#2:

  

2)从产品第一次出现时运行报告,到+ 6个月(查看订单的前6个月)即:小工具1(第一个订单)+从订单日期开始的6个月。类似的东西:
  where widget=widgetID AND date between widget1's first purchase and +6 months

答案:

-- Use for specific comparisons of products OR for the first X months of sales
        where (T3.products_id = 39) and T5.date_purchased between T1.products_date_added and DATE_ADD((T1.products_date_added), INTERVAL 2 MONTH)
    --  This results in PID: release: 10th July, +2 months; 10th Sep. 31 units.

    OR
    -- (T3.products_id = 11 or T3.products_id = 39) gives the results of the 2 product orders from release date to the first 2 months of each
    -- (T3.products_id) gives all products, their first 2 months of release
    -- (T3.products_id = 39) gives specific product release sales

-- Inspired by: http://stackoverflow.com/questions/28788691/mysql-range-between-dates-first-order-6-months?noredirect=1#comment45853546_28788691

答案 1 :(得分:0)

如果您的解决方案可行,请尝试运行2个单独的查询:
- 一个用于查找第一个订单的日期 - 然后从查询1结果计算+6个月 - 和第二个获得两个日期之间的购买