首页 > 代码库 > MySQL 单笔订单满6个及以上产品且金额>=300赠送优惠券_20161103

MySQL 单笔订单满6个及以上产品且金额>=300赠送优惠券_20161103

活动内容: 单笔订单满6个及以上产品(帽子、浴巾除外),金额满赠300元,即赠送300-10元(除帽子、浴巾外)优惠券一张。
需求数据:满足条件的用户ID
活动时间:11.2-11.5(4天)
活动规则:
① 使用无忧券ID908-913的商户即便满足条件也不参与此次活动
② 参与秒杀、满赠的活动的商户可以参与此次活动
③ 11.2日、3日数据4日下午发放;11、4日、5日订单7日发放

 

1.我们可以给他一个最细的明细,每个订单每个产品下单的情况

SELECT a1.城市,a1.用户ID,b.用户名称,b.用户地址,b.联系电话,DATE(订单日期) AS 订单日期,a1.订单ID,a1.销售员,a1.产品ID,a1.产品名称,SUM(a1.金额) AS 金额
FROM `a003_order` AS a1
LEFT JOIN `a001_resterant` AS b ON a1.用户ID=b.用户ID
WHERE a1.城市=济南 AND a1.订单日期>="2016-11-02" AND a1.订单日期<"2016-11-06" AND a1.金额>0 AND (a1.产品名称 NOT LIKE "%帽子%" OR a1.产品名称 NOT LIKe"%浴巾%" )
GROUP BY a1.订单ID,a1.产品ID

2、给他一个订单汇总,其中订单后在跟随使用优惠券的情况

SELECT a2.城市,a2.用户ID,a2.用户名称,a2.用户地址,a2.联系电话,a2.订单日期,a2.订单ID,a2.销售员,COUNT(a2.产品ID) AS 产品数,SUM(a2.金额) AS 订单额,
    CASE WHEN COUNT(a2.产品ID)>=6 AND SUM(a2.金额)>=300 THEN "产品数>=6&单笔订单大于300" ELSE NULL  END AS 标识,a4.优惠券ID,a4.优惠券名称,a4.优惠券金额
    FROM (
        SELECT a1.城市,a1.用户ID,b.用户名称,b.用户地址,b.联系电话,DATE(订单日期) AS 订单日期,a1.订单ID,a1.销售员,a1.产品ID,a1.产品名称,SUM(a1.金额) AS 金额
        FROM `a003_order` AS a1
        LEFT JOIN `a001_resterant` AS b ON a1.用户ID=b.用户ID
        WHERE a1.城市=济南 AND a1.订单日期>="2016-11-02" AND a1.订单日期<"2016-11-06" AND a1.金额>0 AND (a1.产品名称 NOT LIKE "%帽子%" OR a1.产品名称 NOT LIKE
        "%浴巾%" )
        GROUP BY a1.订单ID,a1.产品ID
    ) AS a2
    LEFT JOIN  (#未使用优惠券ID 908到913的用户订单
        SELECT a3.用户ID,a3.订单号,a3.优惠券ID,a3.优惠券名称,SUM(优惠券金额) AS 优惠券金额
        FROM `a016_order_customercoupon_xref` AS a3  
        WHERE a3.城市="济南" AND a3.使用时间>="2016-11-02" AND a3.使用时间<"2016-11-06" AND (a3.优惠券ID<908 OR a3.优惠券ID>913)
        GROUP BY a3.订单号
    ) AS a4 ON a4.订单号=a2.订单ID
    GROUP BY a2.订单ID

3、最后对达标的餐馆再进行汇总group by    having 条件筛选单笔订单符合条件的

SELECT a.城市,a.用户ID,a.用户名称,a.用户地址,a.联系电话,c.销售员,SUM(a.订单额) AS 4天内订单额,COUNT(a.订单ID) AS 4天内订单数,
CASE WHEN SUM(a.订单额)>=300 THEN "10元(除帽子、浴巾外)优惠券一张" ELSE NULL END AS 赠送优惠券
FROM (
    SELECT a2.城市,a2.用户ID,a2.用户名称,a2.用户地址,a2.联系电话,a2.订单日期,a2.订单ID,a2.销售员,COUNT(a2.产品ID) AS 产品数,SUM(a2.金额) AS 订单额,
    CASE WHEN COUNT(a2.产品ID)>=6 AND SUM(a2.金额)>=300 THEN "产品数>=6&单笔订单大于300" ELSE NULL  END AS 标识,a4.优惠券ID,a4.优惠券名称,a4.优惠券金额
    FROM (
        SELECT a1.城市,a1.用户ID,b.用户名称,b.用户地址,b.联系电话,DATE(订单日期) AS 订单日期,a1.订单ID,a1.销售员,a1.产品ID,a1.产品名称,SUM(a1.金额) AS 金额
        FROM `a003_order` AS a1
        LEFT JOIN `a001_resterant` AS b ON a1.用户ID=b.用户ID
        WHERE a1.城市=济南 AND a1.订单日期>="2016-11-02" AND a1.订单日期<"2016-11-06" AND a1.金额>0 AND (a1.产品名称 NOT LIKE "%帽子%" OR a1.产品名称 NOT LIKE
        "%浴巾%" )
        GROUP BY a1.订单ID,a1.产品ID
    ) AS a2
    LEFT JOIN  (#未使用优惠券ID 908到913的用户订单
        SELECT a3.用户ID,a3.订单号,a3.优惠券ID,a3.优惠券名称,SUM(优惠券金额) AS 优惠券金额
        FROM `a016_order_customercoupon_xref` AS a3  
        WHERE a3.城市="济南" AND a3.使用时间>="2016-11-02" AND a3.使用时间<"2016-11-06" AND (a3.优惠券ID<908 OR a3.优惠券ID>913)
        GROUP BY a3.订单号
    ) AS a4 ON a4.订单号=a2.订单ID
    GROUP BY a2.订单ID
    HAVING COUNT(a2.产品ID)>=6 AND SUM(a2.金额)>=300
) AS a
LEFT JOIN `a003b_order_item` AS c ON c.用户ID=a.用户ID
GROUP BY a.用户ID
ORDER BY SUM(a.订单额) DESC 

这样数据对起来也比较方便 可以很方便的看一个用户是否是满足条件达标

MySQL 单笔订单满6个及以上产品且金额>=300赠送优惠券_20161103