CakePHP翻译:获得组的最新日期


CakePHP Translate: Getting group by latest date

如何将下面的代码翻译为cakePHP ?下面的代码来自于一个解决方案:MySQL order by before group by.

使用group by获取作者的最新文章。

 SELECT p1.* 
    FROM wp_posts p1
    INNER JOIN
    (
        SELECT max(post_date) MaxPostDate, post_author
        FROM wp_posts
        WHERE post_status='publish'
           AND post_type='post'
        GROUP BY post_author
    ) p2
      ON p1.post_author = p2.post_author
      AND p1.post_date = p2.MaxPostDate
    WHERE p1.post_status='publish'
      AND p1.post_type='post'
    order by p1.post_date desc

下面仍然是类似的情况:

SELECT t1.* FROM payment_status t1
  JOIN (SELECT payment_id, MAX(created) max_created
        FROM payment_status
        GROUP BY payment_id
        ) t2
    ON t1.payment_id = t2.payment_id AND t1.created = t2.max_created;

我需要一些cakePHP翻译的两个mySQL语句。

------------------------------------------------------------------------------------


我做了一些类似下面的代码,但它给了我错误

Error: SQLSTATE[42000]: Syntax Error or access violation: 1059 Identifier name 'SELECT max(dateEncoded) maxDate, findings FROM maintenance GROUP BY computer_id Office。main_office LIKE'太长

我如何修复它?

 $this->Computer->unbindModel(array(
                'belongsTo' => array('Office'),
                'hasMany' => array('Brand','Maintain')
            ));

            $model_view = $this->Computer->bindModel(array(
                    'hasOne' => array(
                        'Office' => array(
                            'foreignKey' => false,
                            'conditions' => array('Office.id = Computer.office_id')
                        ),
                        'Maintain' => array(
                            'foreignKey' => false,
                            'conditions' => array('Computer.id = Maintain.computer_id'),
                        )
                    )
                )
            );
            $main_office = trim($this->request->data['Office']['office_id']);
            $joins = array(
                array(
                    'table' => "SELECT max(dateEncoded) maxDate, findings FROM maintain GROUP BY computer_id",
                    'alias' => 'P2',
                    'type' => 'INNER',
                    'conditions' => array('Maintain.findings = p2.findings','Maintain.dateEncoded = p2.maxDate')
                )
            );
            $conditions=array("Office.main_office LIKE"=>"%$main_office%");

            $result = $this->Computer->find('all',array(
                $model_view,
                'joins'=>$joins,
                'conditions'=>$conditions,
                'order' =>  array('Office.description'),
                'group' =>  'Computer.id'
            ));

可以这样写:-

$joins = array(
                array(
                    'table' => 'SELECT max(post_date) MaxPostDate, post_author FROM wp_posts WHERE post_status='publish' AND post_type='post'GROUP BY post_author',
                    'alias' => 'P2',
                    'type' => 'INNER',
                    'conditions' => array('WpPost.post_author = p2.post_author','WpPost.post_date = p2.MaxPostDate')
                )
            );
$conditions=array("WpPost.post_status='publish'","WpPost.post_type='post'");
$this->WpPost->find('all',array('fields'=>array('WpPost.*'),'joins'=>$joins,'conditions'=>$conditions);