问题描述
我正在将我的 Web 应用程序从 zf1 移动到 zf2,在我遇到的 sql 查询问题中,我不知道如何建立联合.
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.
我曾经可以制作
$select->union($select1, $select2, $select3)
但是 Zend\Db\Sql\Select 不再有 union()
方法.
but the Zend\Db\Sql\Select does not have an union()
method anymore.
还有没有办法在 zf2 的查询中建立联合?
Is there still a way to make an union in a query with zf2?
推荐答案
我使用这个 https://github.com/zendframework/zf2/pull/3962.(两个选择查询的联合).
I use this https://github.com/zendframework/zf2/pull/3962. (union of two select queries).
为了完成这个答案,我用它来组合/联合 3 个选择:
Just to complete this answer, I use this to combine/union of 3 selects :
$Sql = new Sql ( $adapter );
$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);
小心排序,不要使用 combine !参见 ZF2 联合 + 分页
Be careful order by not work with combine ! see ZF2 Union + Pagination
这篇关于在 zf2 查询中选择联合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!