如何在zend框架中使用sql连接三个表

时间:2016-12-29 11:57:15

标签: php sql zend-framework

我有三个名为:员工,产品和出价的表。

下面的员工表结构

id | employee_id | isid | ename| email | password |admin |practice| phone

出价表结构

bid_id | product_id | employee_id | bid_amount | LastUpdate

产品表结构

product_id |employee_id |name|type|brand|model |condition|about|verified|reserved_price

现在,我想编写一个查询,以便从“出价”表中获取“products”和“bid_id”“bid_amount”中的所有值。然后,来自“employees”表的员工姓名(ename)。如何在Zend框架结构中编写SQL查询? 之前我已经编写了一个查询,以从出价表中获取所有“产品”表格值以及相应的“bid_id”和“bid_amount”值。 现在我想显示相应的员工姓名(ename)。所以我需要加入“员工”表。 任何人都可以帮我这样做吗? 我之前对所有“产品”表格值的查询以及出价表格中的“bid_id”“bid_amount”如下所示。它运作得很好。

 public function fetchAllProductItems() {
    $oSelect = $this->select()
            ->setIntegrityCheck(false)
            ->from(array("p" => "products","b" => "bid"), ('*'))
            ->joinLeft(array("b" => "bid"), "b.product_id=p.product_id", array('bid_id','bid_amount'))
            ->group('p.product_id')
            ->having("p.verified = ?", "Yes");

    return $oSelect;


}   

1 个答案:

答案 0 :(得分:0)

return $this->select()
    ->setIntegrityCheck(false)
    ->from(['p' => 'products'],['*'])
    ->joinLeft(['b' => 'bid'],'b.product_id=p.product_id', ['bid_id','bid_amount'])
    ->joinInner(['e' => 'employees'], 'e.employee_id=b.employee_id',['ename'])
    ->where("p.verified = 'Yes'")
    ->group('p.product_id');

return $this->select()
    ->setIntegrityCheck(false)
    ->from(['b' => 'bid'],['bid_id','bid_amount'])
    ->joinInner(['p' => 'products'],'p.product_id=b.product_id',[
        'product_id',
        'product_name' => 'name',
        'product_type' => 'type',
        'product_brand' => 'brand',
        'product_model' => 'model',
        //etc
        ])
    ->joinInner(['e' => 'employees'],'e.employee_id=b.employee_id',[
        'employee_id',
        'employee_name' => 'ename'
        ])
    ->where("p.verified = 'Yes'");