mysql> SELECT * FROM `log_customer` WHERE `customer_id` = 224 LIMIT 0, 30;
+--------+------------+-------------+---------------------+-----------+----------+
| log_id | visitor_id | customer_id | login_at | logout_at | store_id |
+--------+------------+-------------+---------------------+-----------+----------+
| 817 | 50139 | 224 | 2011-03-21 23:56:56 | NULL | 1 |
| 830 | 52317 | 224 | 2011-03-27 23:43:54 | NULL | 1 |
| 1371 | 136549 | 224 | 2011-11-16 04:33:51 | NULL | 1 |
| 1495 | 164024 | 224 | 2012-02-08 01:05:48 | NULL | 1 |
| 2130 | 281854 | 224 | 2012-11-13 23:44:13 | NULL | 1 |
+--------+------------+-------------+---------------------+-----------+----------+
5 rows in set (0.00 sec)
mysql> SELECT * FROM `customer_entity` WHERE `entity_id` = 224;
+-----------+----------------+---------------------------+----------+---------------------+---------------------+
| entity_id | entity_type_id | email | group_id | created_at | updated_at |
+-----------+----------------+---------------------------+----------+---------------------+---------------------+
| 224 | 1 | damodar@stackoverflow.com.au | 3 | 2011-03-21 04:59:17 | 2012-11-13 23:46:23 |
+-----------+----------------+---------------------------+----------+--------------+----------+-----------------+
1 row in set (0.00 sec)
如何搜索过去10个月未登录且过去10个月内帐户未更新的客户。我在下面尝试但失败了。
$collection = Mage::getModel('customer/customer')->getCollection();
$collection->getSelect()->joinRight(array('l'=>'log_customer'), "customer_id=entity_id AND MAX(l.login_at) <= '" . date('Y-m-d H:i:s', strtotime('10 months ago')) . "'")->group('e.entity_id');
$collection->addAttributeToSelect('*');
$collection->addFieldToFilter('updated_at', array(
'lt' => date('Y-m-d H:i:s', strtotime('10 months ago')),
'datetime'=>true,
));
$collection->addAttributeToFilter('group_id', array(
'neq' => 5,
));
上表有一位客户供参考。我不知道如何在连接上使用MAX()。感谢
这似乎返回了正确的数据,但是我想使用资源集合进行magento方式,所以我不需要再次为for循环加载客户。
$read = Mage::getSingleton('core/resource')->getConnection('core_read');
$sql = "SELECT e.*,MAX(l.login_at) as login_at
FROM `customer_entity` e
LEFT JOIN `log_customer` l on e.entity_id=l.customer_id
GROUP BY l.customer_id
HAVING created_at < '".date('Y-m-d H:i:s', strtotime('10 months ago'))."'
and (login_at< '".date('Y-m-d H:i:s', strtotime('10 months ago'))."' or login_at is null)
and group_id != 5
ORDER BY `e`.`entity_id` ASC";
$result = $read->fetchAll($sql);
答案 0 :(得分:1)
您可以在where过滤器中进行子选择。尝试:
<?php
// Get the customer collection
$collection = Mage::getModel('customer/customer')->getCollection();
$collection
->addFieldToFilter('created_at', array('lt' => date('Y-m-d H:i:s', strtotime('10 months ago'), 'datetime' => true);
// Get the SQL connection and build a Zend_Db_Select subquery so that
// we can filter on customer logins
$read = Mage::getSingleton('core/resource')->getConnection('core_read');
$select = $read->select();
$select
->from('log_customer', array('customer_id'))
->where('login_at < DATE_SUB(CURDATE() INTERVAL 10 MONTHS)');
// Add the subquery to the field filter
$collection
->addFieldToFilter('entity_id', array('in' => $select));