0

我正在尝试选择所有客户购买的最后一个订单,真正的目的是添加一个特定范围,例如过去 3 个月,并考虑过去 3 个月内任何客户的最后一个订单,但没有比这些订单更早的订单3个月,到目前为止我有这个:

SELECT zen_orders.orders_id, zen_customers.customers_id,
    zen_orders.date_purchased,  zen_orders.orders_status
FROM  zen_orders 
INNER JOIN zen_customers
ON zen_orders.customers_id = zen_customers.customers_id
    AND zen_orders.orders_status = '3'
ORDER BY zen_customers.customers_id

我对mysql很陌生,现在这会返回来自所有客户的所有订单,当一个客户超过一个订单时,它会重复几行具有不同订单ID但相同客户ID的行,但现在我如何隔离最后一个从所有有订单的客户那里订购,以便我可以将它放在一个范围内?

4

2 回答 2

0
select o.orders_id,o.customers_id,
    o.date_purchased,  o.orders_status from
(
    select max(o.orders_id) maxOrder,o.customer_id
    FROM  zen_orders o
    where o.orders_status = '3'
    group by o.customers_id
) t
inner join zen_orders o on o.orders_id = t.maxOrder
于 2013-10-15T20:22:09.743 回答
0

显示超过 30 天且不超过 60 天的客户信息和订单信息。

 SELECT C.customers_id, C.customers_firstname, C.customers_lastname,  
    O.date_purchased, O.orders_id, O.order_total, OS.orders_status_name
        FROM  orders O
        LEFT join customers C on C.customers_id = O.customers_id
        LEFT join orders_status OS on OS.orders_status_id=O.orders_status
        WHERE 
           O.date_purchased BETWEEN DATE_SUB(NOW(), INTERVAL 2 MONTH) AND DATE_SUB(NOW(), INTERVAL 1 MONTH)
 GROUP BY C.customers_id
于 2014-01-17T19:43:20.990 回答