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

时间:2011-07-13 07:03:32

标签: php 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') 

我还需要弄清楚如何跟踪结果中的总行数。

4 个答案:

答案 0 :(得分:6)

您需要将Zend_Db_Select数组传递给union()方法。要传递给union()方法以执行UNION类型的第二个参数。在您的情况下,请使用Zend_Db_Select::SQL_UNION_ALL作为常量。

例如

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

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

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

答案 1 :(得分:4)

来自API Docs

  

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

     

向查询添加UNION子句。

     

第一个参数必须是Zend_Db_Select或sql查询字符串的数组。

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

可以在ZF参考指南的Example #28 Example of union() method中找到相同的示例和一些其他文本。除此之外,当您不需要查询生成器时,您始终可以使用Zend_Db_Expr

答案 2 :(得分:2)

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

例如:

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

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

echo $select;

结果:( SELECT A FROM TABLE1)UNION ALL(SELECT A FROM TABLE2)。

答案 3 :(得分:0)

这是正在运行的示例

原始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

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

希望它能帮助某人