如何从对应于从zend框架中的不同表中获取的id的表中获取名称

时间:2017-01-31 10:08:18

标签: sql zend-framework

我在php zend框架中写了一个查询,我加入了三个表员工,产品,出价。 在产品表中我有一个列买家,其中保存买家员工ID,并显示卖家名称的详细信息(取自员工名称,列名称ename),还需要通过从产品表中获取买家ID并显示匹配来显示买家名称员工表中的ename。 我的SQL查询如下。 除上述功能外,其工作正常。

$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'))
            ->joinInner(array("e" => "employees"), "e.employee_id=p.employee_id",array('ename'))
            ->where("p.verified = ?", "Yes")
            ->where("p.sold_out = ?", "Yes")
            ->group('p.product_id')
            ->having("p.sale_end_date >= ?", date("Y-m-d"));

所以任何人都可以帮我将上述功能添加到此查询中。

1 个答案:

答案 0 :(得分:0)

Select query will be like this for displaying buyername

$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'))
            ->joinLeft(array("e" => "employees"), "e.employee_id=p.employee_id",array('ename'))
->joinLeft(array("e1" => "employees"), "e1.employee_id=p.buyer_id",array('buyer_name' => 'ename'))
            ->where("p.verified = ?", "Yes")
            ->where("p.sold_out = ?", "Yes")
            ->group('p.product_id')
            ->having("p.sale_end_date >= ?", date("Y-m-d"));