2014-03-25 40 views
0

我想申請在Zend_Db.My查詢3個表連接查詢是如下:Zend_Db的連接查詢

$id_array = array("1","2"); 

$query = $this->select(); 
$query->from(array('b' => 'brands'), array('b.brand_id','b.brand_name'))->where('b.brand_id NOT in (?)', $id_array)->order('RAND()')->limit(5); 
$query->join(array('p' => 'product'), 'b.brand_id = p.brand_id', array('p.product_id', 'p.product_price')); 
$query->join(array('pimg' => 'product_img_map'), 'p.product_id = pimg.product_id', array('pimg.img_location')); 
$query->setIntegrityCheck(false); 
$resultRows = $this->fetchAll($query); 
return $resultRows; 

在一個品牌可能有一個以上的產品,但在查詢本人申請限制品牌表即爲5.默認情況下它也適用於產品,也因爲如果一個品牌有5個產品它只是給出一個品牌的信息。對此有任何建議。 謝謝。

回答

0

你可以使用子查詢來獲得想要的結果:

$id_array = array("1","2"); 

$subQuery = $this->select() 
    ->from(array('b' => 'brands'), array('b.brand_id','b.brand_name')) 
    ->where('b.brand_id NOT in (?)', $id_array) 
    ->order('RAND()') 
    ->limit(5); 
$query = $this->select() 
    ->from(array('b' => $subQuery), array('*')) 
    ->join(array('p' => 'product'), 'b.brand_id = p.brand_id', array('p.product_id', 'p.product_price')) 
    ->join(array('pimg' => 'product_img_map'), 'p.product_id = pimg.product_id', array('pimg.img_location'));