简体   繁体   中英

sql query with union in zend framework

  Table : sc_message 
  ______________________________________________________________________
 |message_id | message_sender_id | message_receiver_id | message_content|
 ------------------------------------------------------------------------  

Table : sc_user 
  _____________________
 | user_id | user_name |
 ----------------------- 

Table : sc_message_slave

| message_slave_id | message_slave_sender_id | message_slave_receiver_id | message_slave_content | message_slave_sent_on |

 $Query_1 = $this ->select()
                  ->from(array('msg' => 'sc_message'), array('msg.message_sender_id', 'msg.message_receiver_id', 'msg.message_content', 'msg.message_sent_on'))
                  ->join(array('usr' => 'sc_user'), 'msg.message_sender_id = usr.user_id', array('usr.user_name as sender_name'))
                  ->where('msg.message_id = ?',$message_id)
                  ->setIntegrityCheck(false);


 $Query_2 = $this ->select()
                     ->from(array('msg_slv' => 'sc_message_slave'), array('msg_slv.message_slave_sender_id', 'msg_slv.message_slave_receiver_id','msg_slv.message_slave_content', 'msg_slv.message_slave_sent_on'))
                     ->join(array('usr' => 'sc_user'), 'msg_slv.message_slave_sender_id = usr.user_id', array('usr.user_name as sender_name'))
                     ->where('msg_slv.message_id = ?',$message_id)
                     ->setIntegrityCheck(false);            
 $select = $this ->select()
                    ->union(array($Query_1, $Query_2))
                    ->order('msg.message_sent_on')
                    ->setIntegrityCheck(false);

It gives me warning like this and execution stops there ..

Warning: Invalid use of table with UNION in E:\wamp\www\social_site\library\Zend\Db\Select.php on line 1222 

can you please tell me what is wrong with the query?

Since you have an order by where you do the union, you probably ran into this issue:

http://framework.zend.com/issues/browse/ZF-4338

They won't fix the issue, you can read as to why, but someone posted a work around.

$select_1 = $db->select()->...;
$select_2 = $db->select()->...;

$main_select = $db->select()->union( array(  '('.$select_1.')',  '('.$select_2.')' ) );

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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