【问题标题】:Cake PHP3 subquery in Join statementJoin 语句中的 Cake PHP3 子查询
【发布时间】:2017-12-06 08:19:03
【问题描述】:

我需要使用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

以蛋糕的方式。谢谢。

【问题讨论】:

    标签: php mysql cakephp pagination cakephp-3.0


    【解决方案1】:

    你可以试试这个吗?自然加入在这里不起作用。不过我稍后会看看。谢谢。

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

    【讨论】:

    • 感谢您的解决方案,它显示了路径,但我需要为我的问题实施Natural Join。有什么方法可以在 Cake PHP 3 中实现 Natural Join 吗?我可以通过以下方式做到这一点,但没有 conditions 可能吗? ` 't' => [ 'table' => '(SELECT chat_id, MAX(id) AS id FROM message GROUP BY chat_id)', 'type' => 'Natural', ],`
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-09-23
    • 1970-01-01
    • 2012-05-16
    • 1970-01-01
    • 2021-10-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多