在zend框架中使用union进行sql查询

时间:2012-04-16 08:24:39

标签: php mysql zend-framework zend-db zend-db-table

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

它给了我这样的警告,并且执行停在那里..

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

你能告诉我这个查询有什么问题吗?

1 个答案:

答案 0 :(得分:1)

由于你有一份订单,你可能会遇到这个问题:

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

他们不会修复这个问题,你可以阅读为什么,但有人发布了一个解决方案。

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

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