关于 Zend 框架:Zend_Db_Select:Zend 框架:Zend_Db_Select – 如何加入自定义子查询表?

Zend Framework: Zend_Db_Select - how to join custom subquery table?

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

这样不行。子查询进入引号。

这样:

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

谢谢 ;)


使用

1
2
3
4
5
$select->joinRight(
    array('i' => new Zend_Db_Expr('(SELECT * FROM images ORDER BY image_id)')),
    'i.ad_id = '. $main .'.id',
    $imarray
);


我觉得这更容易阅读和浏览...

1
2
3
4
5
6
7
8
9
10
11
      $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);