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);