2011-11-18 67 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);

將此用於連接條件。您可以使用任何其他表格以及滿足您的要求的條件。