簡體   English   中英

如何在這里使用JOIN查詢?

[英]How to use JOIN query here?

我正在使用zend。 我在首頁中實現了特色產品。 現在我想在每個產品下顯示評論,可能沒有對所有產品進行評論,我需要使用查詢來獲取具有評論等級和沒有評論等級的產品。

下面是我的代碼,

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;
}

當我執行以上操作時,它僅獲取具有評價等級的產品,我也想獲得沒有評價等級的產品嗎?

請幫助我。

您必須做一個左聯接。 使用$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

受保護的$ _user_profile =“ user_profile”; //您的表格名稱

受保護的$ _app_functionalities_profile =“ app_functionalities_profile”; //其他表名

$ select = $ this-> select()

-> setIntegrityCheck(假)

->從($ this-> _ user_profile)

->從($ this-> _ app_functionalities_profile)

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

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

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

使用此條件作為連接條件。您可以使用任何其他表以及滿足條件的條件。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM