列出过去12个月未订购的Magento客户

时间:2015-03-25 23:28:48

标签: mysql magento filter orders

在Magento v1.9中,我需要一个MYSQL选择,它将执行以下操作:

  • 列出过去12个月内未下订单的所有客户

谢谢!

1 个答案:

答案 0 :(得分:3)

基本上,您希望从未下过订单的客户或最后订单超过12个月的客户。 您可以使用客户集合来执行此操作:

$date = new DateTime();
$date->sub(new DateInterval('P12M'));
$customers = Mage::getModel('customer/customer')->getCollection();
$customers->getSelect()->joinLeft(
    array('o' => Mage::getSingleton('core/resource')->getTableName('sales/order')),
    'o.customer_id = e.entity_id',
    array(
        'last_order_date' => 'MAX(o.created_at)',
    )
);
$customers->groupByAttribute('entity_id')
    ->getSelect()
    ->having('last_order_date < ?',$date->format('Y-m-d'))
    ->orHaving('last_order_date IS NULL');

或者如果你想要那个只调用的mysql $customers->getSelect();

这将为您提供以下MYSQL查询

SELECT `e`.*, MAX(o.created_at) AS `last_order_date` FROM `customer_entity` AS `e` LEFT JOIN `sales_flat_order` AS `o` ON o.customer_id = e.entity_id WHERE (`e`.`entity_type_id` = '1') GROUP BY `e`.`entity_id` HAVING (last_order_date < '2014-03-26') OR (last_order_date IS NULL)

希望有所帮助!