【问题标题】:CakePHP find with MAX and group byCakePHP 使用 MAX 查找并分组
【发布时间】:2017-10-17 19:04:55
【问题描述】:

我有一个包含多条用户记录的表,例如他们的登录日期和 IP 地址。

所以,我想获取我数据库中每个用户的最后一个 IP 地址或最后登录日期,最后n 天。

当我尝试在 cakephp find 中使用"group" 时,它没有显示正确的记录:

$data = $this->Login->find('all',
    array(
          'conditions'=>array('Login.last_login_date<='=>$d),
          'group'=>'Login.user_id',
          'order'=>array('Login.last_login_date'=>'DESC'),
          'fields' => array('MAX(Login.last_login_date) AS last_login_date', '*','User.*')
    )
);

这可能是什么原因?

【问题讨论】:

标签: mysql cakephp cakephp-2.0


【解决方案1】:

通过按登录日期 desc 进行排序,您走在了正确的轨道上。您实际上不需要在这里找到登录日期的MAX。如果要获取上次登录日期,只需按登录日期降序排列即可。分组后的登录日期值将是最后登录日期。

$data = $this->Login->find('all', array(
    // Assuming that the login date column is login_date here
    'conditions' => array('Login.login_date <=' => $d), 
    'group' => 'Login.user_id',
    'order' => array('Login.login_date' => 'DESC'), 
    'fields' => array('*', 'User.*')
));

【讨论】:

    猜你喜欢
    • 2012-01-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-06-30
    相关资源
    最近更新 更多