在zf2查询中选择union

时间:2012-11-30 16:59:43

标签: zend-framework2

我正在将我的Web应用程序从zf1移动到zf2以及我对sql查询的问题,我无法弄清楚如何建立联合。

我曾经能够制作

$select->union($select1, $select2, $select3) 

但Zend \ Db \ Sql \ Select不再使用union()方法。

还有办法在zf2的查询中建立联合吗?

3 个答案:

答案 0 :(得分:6)

我使用此https://github.com/zendframework/zf2/pull/3962。 (两个选择查询的联合)。

为了完成这个答案,我用它来组合/联合3个选择:

$Sql = new Sql ( $adapter );

    $select1 = $Sql->select();
    $select1->from(...);
    $select2 = $Sql->select();
    $select2->from(...);
    //union of two first selects
    $select1->combine ( $select2 );
    //create the third select

    $select3 = $Sql->select();
    $select3->from(...);

    //Final select

    $select = $Sql->select();
    $select->from(array('sel1and2' => $select1));
    $select->combine ( $select3 );
    $select->order($order);

$select1 = $Sql->select(); $select1->from(...); $select2 = $Sql->select(); $select2->from(...); //union of two first selects $select1->combine ( $select2 ); //create the third select $select3 = $Sql->select(); $select3->from(...); //Final select $select = $Sql->select(); $select->from(array('sel1and2' => $select1)); $select->combine ( $select3 ); $select->order($order);

小心顺序不使用联合收割机!见ZF2 Union + Pagination

答案 1 :(得分:4)

作为那些想要轻松使用> 1个UNION的人,ZF2有一个专用的课程Zend\Db\Sql\Combine

new Combine(
    [
        $select1,
        $select2,
        $select3,
        ...
    ]
)

(new Combine)->union($select);

答案 2 :(得分:0)

以下是由UNION组合的四个SELECT的“重型解决方法”示例:

$sm = $this->getServiceLocator();
$adapter = $sm->get('Zend\Db\Adapter\Adapter');

$columns = array(
    'date', 'business_unit', 'project', 'cost_center',
    'some_value1' => new Expression('SUM(IF(recruitment = \'internal\',1,0))'),
    'some_value2' => new Expression('SUM(IF(recruitment = \'external\',1,0))'),
    'some_value3' => new Expression('SUM(total_employees)'),
    'some_value4' => new Expression('SUM(IF(recruitment = \'internal\',total_time,0))'),
    'some_value5' => new Expression('SUM(IF(recruitment = \'external\',total_time,0))'),
    'some_value6' => new Expression('SUM(total_time)')
);

$sql = new Sql($adapter);

$abstractSelect = $sql->select();
$abstractSelect->from('summary')
    ->columns($columns)
    ->where->equalTo('date', '2013-01-25')
    ->where->equalTo('business_unit', 'foo')
    ->where->equalTo('project', 'bar');

$select1 = clone($abstractSelect);

$select2 = clone($abstractSelect);
$select2->where->equalTo('attendant', '1');

$select3 = clone($abstractSelect);
$select3->where->equalTo('attendant', '0')
    ->where->equalTo('recruitment', 'internal');

$select4 = clone($abstractSelect);
$select4->where->equalTo('attendant', '0')
    ->where->equalTo('disbursal', '0');

$selects = array(
    $select1->getSqlString(),
    $select2->getSqlString(),
    $select3->getSqlString(),
    $select4->getSqlString()
);      

$union = implode(" UNION ", $selects);

$statement = $adapter->createStatement(str_replace('"', '`', $union));
$results = $statement->execute();

这不会赢得任何代价,但也许有人可以改善这一点。

最后两行很重要。您可以解析任何您喜欢的UNION查询。