Php 如何在这里使用连接查询?

Php 如何在这里使用连接查询?,php,mysql,zend-framework,zend-db,Php,Mysql,Zend Framework,Zend Db,我正在使用zend。我在主页上实现了特色产品。现在我想显示每个产品下的评论。所有产品的评论可能会给出,也可能不会给出。我需要使用查询来获取有评论评级和没有评论评级的产品 下面是我的代码 public function funFutureProduct($option = array()) { $start = isset($option['start']) ? $option['start'] : 0; $limit = isset($option['limit']

我正在使用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()

Zend\u Db\u Select的默认
join()
方法假定您要执行
内部联接

受保护的$\u user\u profile=“user\u profile”;//您的桌名

受保护的$应用程序功能\u profile=“应用程序功能\u profile”//其他表名

$select=$this->select()

->setIntegrityCheck(假)

->来自($this->\u user\u profile)

->从($此->\应用程序\功能\配置文件)

->其中('USER\u PROFILE.USER\u id=?',1)

->其中('USER_PROFILE.PROFILE_id=app_functionaties_PROFILE.PROFILE_id')

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

将此用于联接条件。您可以使用任何其他表和where条件来满足您的要求

->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)')))