在Join语句中加入PHP3子查询

时间:2017-07-03 06:39:43

标签: php mysql cakephp pagination cakephp-3.0

我需要使用cake php方法来构建此自定义查询,但是这可能会以蛋糕搁浅的方式进行此子查询吗?

SELECT
                    m.chat_id AS chat_id,
                    m.message AS message,
                    m.created AS created,
                    g.title AS title,
                    u.id AS id,
                    u.name AS name,
                    g.id AS gig_id
                  FROM message m
                  NATURAL JOIN (
                    SELECT   chat_id, MAX(id) AS id
                    FROM     message
                    GROUP BY chat_id
                  ) t
                  RIGHT JOIN chat c ON c.id = t.chat_id
                  INNER JOIN application a ON a.chat_id = m.chat_id
                  INNER JOIN gig g ON g.id = a.gig_id
                  INNER JOIN user u ON u.id = a.user_id
                  WHERE (g.status = 1) 
                  ORDER BY m.created DESC

我的代码如下所示

$this->loadModel('Message');
        $dataField = $this->Message->find()
            ->hydrate(false)
            ->select(['Message.chat_id', 'Message.message', 'Message.created', 'u.id', 'u.name', 'g.id'])
            ->join([
                'a' => [
                    'table' => 'application',
                    'type' => 'INNER',
                    'conditions' => 'a.chat_id = chat_id',
                    ],
                'g' => [
                    'table' => 'gig',
                    'type' => 'INNER',
                    'conditions' => 'g.id = a.gig_id',
                    ],
                'u'  => [
                    'table' => 'user',
                    'type' => 'INNER',
                    'conditions' => 'u.id = a.user_id',
                ],

            ])
            ->where(['g.status' => 1])
            ->order(['Message.created' => 'DESC']);

我需要知道实施

NATURAL JOIN (
                    SELECT   chat_id, MAX(id) AS id
                    FROM     message
                    GROUP BY chat_id
                  ) t
                  RIGHT JOIN chat c ON c.id = t.chat_id

以蛋糕的方式。感谢。

1 个答案:

答案 0 :(得分:4)

你能试试吗?自然加入不在这里工作。我稍后会看看。感谢。

$this->loadModel('Message');
$query = $this->Message->find()
    ->hydrate(false)
    ->select([
        'Message.chat_id',
        'Message.message',
        'Message.created',
        'u.id',
        'u.name',
        'g.id',
        't.id',
        't.chat_id'
    ])
    ->join([
        'a' => [
            'table'      => 'application',
            'type'       => 'INNER',
            'conditions' => 'a.chat_id = Message.chat_id',
        ],
        'g' => [
            'table'      => 'gig',
            'type'       => 'INNER',
            'conditions' => 'g.id = a.gig_id',
        ],
        'u' => [
            'table'      => 'user',
            'type'       => 'INNER',
            'conditions' => 'u.id = a.user_id',
        ],
        't' => [
            'table'      => '(SELECT chat_id, MAX(id) AS id FROM message GROUP BY chat_id)',
            'type'       => 'INNER',
            'conditions' => 't.id > 0',
        ],
        'c' => [
            'table'      => 'chat',
            'type'       => 'RIGHT',
            'conditions' => 'c.id = t.chat_id',
        ]
    ])
    ->where(['g.status' => 1])
    ->order(['Message.created' => 'DESC']);

$this->loadComponent('Paginator');
$paginated = $this->Paginator->paginate($query, ['limit' => 2]);
pr($paginated);
die;