select union in zf2 query

2019-02-07 13:25发布

问题:

I am moving my web application from zf1 to zf2 and among the problems I have with sql queries, I can't figure out how to make a union.

I used to be able to make

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

but the Zend\Db\Sql\Select does not have an union() method anymore.

Is there still a way to make an union in a query with zf2?

回答1:

I use this https://github.com/zendframework/zf2/pull/3962. (union of two select queries).

Just to complete this answer, I use this to combine/union of 3 selects :

$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);

Be careful order by not work with combine ! see ZF2 Union + Pagination



回答2:

As an alternative for those wanting ease with >1 UNIONs, ZF2 has a dedicated class Zend\Db\Sql\Combine:

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

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



回答3:

Here is a "heavy workaround" example for four SELECTs combined by UNION:

$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();

This will win no price but maybe someone can improve on this.

The last two lines are important. You can parse any UNION query you like.