2011-11-18 64 views
0

我正在使用zend。我在主页中实施了特色产品。现在我想在每个产品下展示评论。对于所有产品可能提供或不提供评论。我需要使用查询来获取具有评论等级且没有评论等级的产品。如何在这里使用JOIN查询?

下面是我的代码,

public function funFutureProduct($option = array()) 
{  
    $start = isset($option['start']) ? $option['start'] : 0; 
    $limit = isset($option['limit']) ? $option['limit'] : Zend_Registry::get("featured_prod_row_length"); 
    $oDb=Zend_Registry::get("db");  
    $list_select = $oDb->select() 
     ->from(array('p'=>'product'), array('p.*')) 
     ->join(array('m'=>'manufacturers'),'p.manufacturer_id = m.manufacturer_id', array("manufacture_name"=>'m.name')) 
     ->join(array('s'=>'category'),'s.category_id = p.category_id', array("sub_category_name"=>'s.name')) 
     ->join(array('c'=>'category'),'c.category_id = s.parent', array("category_name"=>'c.name')) 
     ->join(array('p_w'=>'prod_warehouse'),'p_w.product_id = p.product_id', array("curent_stock"=>'p_w.curent_stock')) 
     ->join(array('r'=>'review'),'p.product_id = r.product_id and r.status = 1', array("avg_review_rating"=>new Zend_Db_Expr('AVG(r.rating)'),"count_user_rating"=>new Zend_Db_Expr('COUNT(r.product_id)'))) 
     ->limit($limit, $start); 

    $list_select->where('s.status = 1 AND c.status = 1 AND p.status = 1 AND m.status = 1 AND p.product_id != "" AND p_w.status > 0'); 
    $list_select->group('p.product_id'); 
    $list_select->where('p.featured_product = 1'); 
    $aData=$oDb->fetchall($list_select); 
    return $aData; 
} 

当我执行上面,只取其中有回顾率的产品,我也想要得到它没有审查该产品的额定?

请帮助我。

回答

1

你必须做一个左连接。使用$select->joinLeft()

->joinLeft(array('r'=>'review'),'p.product_id = r.product_id and r.status = 1', array("avg_review_rating"=>new Zend_Db_Expr('AVG(r.rating)'),"count_user_rating"=>new Zend_Db_Expr('COUNT(r.product_id)'))) 

Zend_Db_Select默认join()方法假定你想要做一个INNER JOIN

+0

谢谢,我应该使用左连接。我在想什么..... – mymotherland

+0

不客气。干杯! –

0

protected $ _user_profile =“user_profile”; //您的表名

protected $ _app_functionalities_profile =“app_functionalities_profile”; //其他表名

$选择= $这个 - >选择()

- > setIntegrityCheck(假)

- >从($本 - > _ user_profile)

- >从($这 - > _ app_functionalities_profile)

- >其中( 'USER_PROFILE.user_id =?',1)

- >其中( 'USER_PROFILE.profile_id = app_functionalities_profile.profile_id');

$ userdata = $ this-> fetchAll($ select);

将此用于连接条件。您可以使用任何其他表格以及满足您的要求的条件。