小智 5
基本上,您需要从未下过订单的客户或上次下单时间超过 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');
Run Code Online (Sandbox Code Playgroud)
或者如果你想要 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)
Run Code Online (Sandbox Code Playgroud)
希望有帮助!
| 归档时间: |
|
| 查看次数: |
2645 次 |
| 最近记录: |