简体   繁体   English

PHP Zend 框架 - 我如何使用 union all?

[英]PHP Zend Framework - How do I use union all?

How do I use UNION ALL in this fashion in Zend Framework?如何在 Zend Framework 中以这种方式使用UNION ALL

(select id from astrology where commu_time_from  <= '11:51' and commu_time_to >= '11:51' and user_id=1 and appo_date='03/01/2017') union all
(select id from facereading where commu_time_from  <= '11:51' and commu_time_to >= '11:51' and user_id=1 and appo_date='03/01/2017') union all
(select id from numerology where commu_time_from  <= '11:51' and commu_time_to >= '11:51' and user_id=1 and appo_date='03/01/2017') union all
(select id from palmistry where commu_time_from  <= '11:51' and commu_time_to >= '11:51' and user_id=1 and appo_date='03/01/2017') union all
(select id from solutions where commu_time_from  <= '11:51' and commu_time_to >= '11:51' and user_id=1 and appo_date='03/01/2017') union all
(select id from vastu where commu_time_from  <= '11:51' and commu_time_to >= '11:51' and user_id=1 and appo_date='03/01/2017') 

I also need to figure out how to track the total # of rows in the results.我还需要弄清楚如何跟踪结果中的总行数。

You need to pass an array of Zend_Db_Select into the union() method.您需要将Zend_Db_Select数组传递给 union() 方法。 The second parameter to be passed to the union() method to perform the type of UNION.第二个参数要传递给union()方法来执行 UNION 的类型。 In your case, use Zend_Db_Select::SQL_UNION_ALL as a constant.在您的情况下,使用Zend_Db_Select::SQL_UNION_ALL作为常量。

For eg例如

$sql1 = $db->select();
$sql2 = "SELECT ...";

$select = $db->select()
    ->union(array($sql1, $sql2), Zend_Db_Select::SQL_UNION_ALL );

Reference: http://framework.zend.com/manual/1.12/en/zend.db.select.html#zend.db.select.building.union参考: http://framework.zend.com/manual/1.12/en/zend.db.select.html#zend.db.Z99938282F04071859941E18F16EFCF.

From API Docs :来自API 文档

union( array $select = array, $type = self ): Zend_Db_Select

Adds a UNION clause to the query.向查询中添加 UNION 子句。

The first parameter has to be an array of Zend_Db_Select or sql query strings.第一个参数必须是 Zend_Db_Select 或 sql 查询字符串的数组。

 $sql1 = $db->select(); $sql2 = "SELECT..."; $select = $db->select() ->union(array($sql1, $sql2)) ->order("id");

The same example and some additional text can be found in the Example #28 Example of union() method in the ZF Reference guide.可以在 ZF 参考指南中的Example #28 Example of union() 方法中找到相同的示例和一些附加文本。 Apart from that, you can always use Zend_Db_Expr when you dont need the Query Builder.除此之外,当您不需要查询构建器时,您总是可以使用Zend_Db_Expr

$select = union($select1, $select2, self::SQL_UNION_ALL).

Ex:前任:

$select1 = 'select A from table1';
$select2 = 'select A from table2';

$select = $db->select()->union($select1, $select2, Zend_Db_Select::SQL_UNION_ALL);

echo $select;

Result: (SELECT A FROM TABLE1) UNION ALL (SELECT A FROM TABLE2).结果:(从表 1 中选择 A)联合所有(从表 2 中选择 A)。

here is the running example这是正在运行的示例

in raw Sql在原始 Sql

select CONCAT_WS(' ', u.fname, u.lname) as user_name, u.image, c.* from challenge c inner join challenge_user cu on (cu.challenge_id = c.id) inner join users u on (u.id = c.user_id) where c.user_id = '1' group by c.id

union all 
select CONCAT_WS(' ', u.fname, u.lname) as user_name, u.image, c.* from challenge c inner join challenge_user cu on (cu.challenge_id = c.id) inner join users u on (u.id = c.user_id) where concat('%,',c.challenger_user_id,',%') LIKE concat( '%,"1",%' ) group by c.id

in zend在zend

 $sql1 = $this->select()->setIntegrityCheck(FALSE)
      ->from(array('c'=>'challenge'),array('user_name'=>new Zend_Db_Expr("CONCAT_WS(' ',                      u.fname, u.lname)"),'u.image'))
        ->joinInner(array('cu'=>'challenge_user'), 'cu.challenge_id = c.id')
         ->joinInner(array('u'=>'users'),'u.id = c.user_id')
         ->where('c.user_id = ?','1')
          ->group('c.id');
 $sql2 = $this->select()->setIntegrityCheck(FALSE)->from(array('c'=>'challenge'),array('user_name'=>new Zend_Db_Expr("CONCAT_WS(' ', u.fname, u.lname)"),'u.image'))
               ->joinInner(array('cu'=>'challenge_user'), 'cu.challenge_id = c.id')
               ->joinInner(array('u'=>'users'),'u.id = c.user_id')
               ->where(new Zend_Db_Expr("concat('%,',c.challenger_user_id,',%') LIKE concat( '%,"1",%' )"))->group('c.id');

$select = $this->select()->union(array($sql1,$sql2),  Zend_Db_Select::SQL_UNION_ALL);

$data =  $this->fetchAll($select);

Hope it will help some one希望它会帮助一些人

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM