Zend Framework: Zend_Db_Select - how to join custo

2019-01-24 17:10发布

$select->joinRight(array('i' => '(SELECT * FROM images ORDER BY image_id)'),'i.ad_id = '. $main .'.id',$imarray);

Like that doesn't work. Subquery getting inside quotes.

Like that:

RIGHT JOIN `(SELECT * FROM images ORDER BY image_id)` AS `i` ON i.ad_id = a.id

Thanks ;)

2条回答
一纸荒年 Trace。
2楼-- · 2019-01-24 17:28

Use

$select->joinRight(
    array('i' => new Zend_Db_Expr('(SELECT * FROM images ORDER BY image_id)')),
    'i.ad_id = '. $main .'.id',
    $imarray
);
查看更多
Deceive 欺骗
3楼-- · 2019-01-24 17:43

I feel This is easier to read and navigate...

      $sub = $this->select()
            ->setIntegrityCheck(false)
            ->from(array('i' => 'images'), array('*'))
            ->order('i.image_id');

$select = $this->select()
             ->setIntegrityCheck(false)
             ->from(array('m' => 'MAIN_TABLE'), array('*'))
             ->joinRight(array('i' => $sub), 'i.ad_id = m.id', array('*'));

   return $this->select($select);
查看更多
登录 后发表回答