繁体   English   中英

Magento-如何按最大值选择mysql行?

[英]Magento - How to select mysql rows by max value?

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

我已经将完整的shell脚本加载到github

您可以在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));

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM