如何在zend框架中使用sql连接三个表
问题描述:
我有三个名为employees,products和bid的表。下面如何在zend框架中使用sql连接三个表
员工表结构
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
现在我想编写一个查询从 “产品” 获得的所有值和“bid”表中的“bid_id”“bid_amount”。然后,从“员工”表中选择员工姓名(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;
}
答
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'");
当我用第一个,从产品,其验证字段为是其只读取第一个值。 –