如何在zend 2中进行内连接

时间:2013-11-27 07:55:23

标签: join zend-framework2

我正在尝试在zend2下面进行SQL查询,但我不知道如何制作它。有人有解决方案吗?

SELECT Customer_info.Name, d1.Value, d2.value , Customer_info.Status 
FROM Customer_info
INNER JOIN Dataman as d1
ON d1.module='Customerinfo' and Customer_info.Status=d1.Id  and d1.data_field = 'Status' 
INNER JOIN Dataman as d2
ON d2.module='Customerinfo' and Customer_info.Vip_level=d2.Id  and d2.data_field = 'Vip_level';

1 个答案:

答案 0 :(得分:2)

我已将您的查询转换为ZF2 select语句。请查看以下内容:

// collect the Sql instance.<br />
$sql = new Sql($this->dbSql);

$select = $sql->select();
$select->from('Customer_info')
    ->columns(array(
        'Name',
        'Status'
    ))
    ->join(
        array('t2' => 'Dataman'),
        new \Zend\Db\Sql\Expression("t2.module = 'Customerinfo' and Customer_info.Status=t2.Id  and t2.data_field = 'Status'"),
        array(
            'Value'
        ),
        $select::JOIN_INNER
    )
    ->join(
        array('t3' => 'Dataman'),
        new \Zend\Db\Sql\Expression(" t3.module='Customerinfo' and Customer_info.Vip_level=t3.Id  and t3.data_field = 'Vip_level' "),
        array(
            'value'
        ),
        $select::JOIN_INNER
    );

$resultSet = $this->tableGateway->selectWith($select);

return $resultSet;

谢谢:)