包含日期的复杂查询

发布于 2024-09-13 17:19:47 字数 752 浏览 5 评论 0原文

我想从 Magento 退回前一天创建或前一天更新的客户。我尝试使用 addFieldToFilter,但没有成功。

我也尝试操作 Zend_Db_Select,没有成功。

所以现在我被困住了!

这是我的一些尝试:

$customer = Mage::getModel('customer/customer');
$customers = $customer
    ->getCollection()
    ->getSelect()
    ->where("updated_at >= ? AND updated_at <= ?",$this->getFrom(), $this->getTo())
    ->orWhere("e.created_at >= ? AND e.created_at <= ?", $this->getFrom(), $this->getTo());

或者

->addFieldToFilter(
    array(
        array('attribute'=>'updated_at', 'gteq'=>$this->getFrom()),
        array('attribute'=>'created_at', 'gteq'=>$this->getFrom())
    ),
    '',
    'left'
);

谢谢

I'd like to return the customers from Magento who where created the day before OR who where updated the day before. I tried to play with addFieldToFilter, without any success.

I also tried to manipulate Zend_Db_Select, no success.

So now I'm stuck!

Here are some of my tries :

$customer = Mage::getModel('customer/customer');
$customers = $customer
    ->getCollection()
    ->getSelect()
    ->where("updated_at >= ? AND updated_at <= ?",$this->getFrom(), $this->getTo())
    ->orWhere("e.created_at >= ? AND e.created_at <= ?", $this->getFrom(), $this->getTo());

Or

->addFieldToFilter(
    array(
        array('attribute'=>'updated_at', 'gteq'=>$this->getFrom()),
        array('attribute'=>'created_at', 'gteq'=>$this->getFrom())
    ),
    '',
    'left'
);

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

燃情 2024-09-20 17:19:47

我建议不要直接操作选择,除非绝对必要并且您确切地知道您的 Magento 版本的幕后发生了什么。

以下语法应该为您处理棘手的部分,

$c = Mage::getModel('customer/customer')
->getCollection()
->addAttributeToFilter(array(               
    array('attribute'=>'updated_at','from'=>'2010-05-12','to'=>'2010-05-30'),
    array('attribute'=>'created_at','from'=>'2010-05-12','to'=>'2010-05-13')
));

var_dump( (string) $c->getSelect());
var_dump(count($c));

您所需要做的就是放入您想要的日期范围。

I'd recommend against directly manipulating the select unless it's absolutely necessary and you know exactly what's going on behind the scenes in your version of Magento.

The following syntax should handle the tricky parts for you

$c = Mage::getModel('customer/customer')
->getCollection()
->addAttributeToFilter(array(               
    array('attribute'=>'updated_at','from'=>'2010-05-12','to'=>'2010-05-30'),
    array('attribute'=>'created_at','from'=>'2010-05-12','to'=>'2010-05-13')
));

var_dump( (string) $c->getSelect());
var_dump(count($c));

all you'll need to do is drop in the date ranges you want.

小霸王臭丫头 2024-09-20 17:19:47

使用 updated_at 作为过滤器属性就足够了,因为它在创建用户时设置为当前日期时间。因此,通过使用此字段进行过滤,您将获得新用户和非新用户但在给定时间段内更新的用户。以下是查找过去 24 小时内更新或创建的用户的代码:

$customers = Mage::getModel('customer/customer')->getCollection();
$customers->addAttributeToFilter('updated_at', array('gt' => date("Y-m-d H:i:s", time()-60*60*24)));

foreach($customers as $customer) {
    //do sth
}

It is enough to use updated_at as your filter attribute, because it is set to current datetime when a user is created. So by filtering with this field you will get both new users and those who are not new but were updated in the given period. Here's the code to look for users updated or created during the last 24 hours:

$customers = Mage::getModel('customer/customer')->getCollection();
$customers->addAttributeToFilter('updated_at', array('gt' => date("Y-m-d H:i:s", time()-60*60*24)));

foreach($customers as $customer) {
    //do sth
}
屋檐 2024-09-20 17:19:47

感谢艾伦和西尔沃,这就是我写的:

->addAttributeToFilter(array(               
    array('attribute'=>'updated_at','from'=>$this->getFrom(),'to'=>$this->getTo())
));

两个答案都很有用。谢谢你!

Thanks to Alan and Silvo, here is what I wrote :

->addAttributeToFilter(array(               
    array('attribute'=>'updated_at','from'=>$this->getFrom(),'to'=>$this->getTo())
));

Both answers were usefull. Thank you!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文