0

How do I use UNION ALL in this fashion in Zend Framework?

(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.

0

4 Answers 4

6

You need to pass an array of Zend_Db_Select into the union() method. The second parameter to be passed to the union() method to perform the type of UNION. In your case, use Zend_Db_Select::SQL_UNION_ALL as a constant.

For e.g.

$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

Sign up to request clarification or add additional context in comments.

Comments

4

From API Docs:

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

Adds a UNION clause to the query.

The first parameter has to be an array of Zend_Db_Select or sql query strings.

$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. Apart from that, you can always use Zend_Db_Expr when you dont need the Query Builder.

Comments

2
$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).

Comments

0

here is the running example

in raw 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);

Hope it will help some one

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.