0

我想在 zend 框架中执行以下查询。谁能告诉我该怎么做?

(SELECT `msg`.`message_sender_id`, `msg`.`message_receiver_id`, `msg`.`message_content`, `msg`.`message_sent_on`, `usr`.`user_name` AS `sender_name`
 FROM `sc_message` AS `msg` INNER JOIN `sc_user` AS `usr` ON `msg`.`message_sender_id` = `usr`.`user_id` 
 WHERE `msg`.`message_id` = 3 ORDER BY `msg`.`message_sent_on`)

UNION 

(SELECT `msg_slv`.`message_slave_sender_id`, `msg_slv`.`message_slave_receiver_id`, `msg_slv`.`message_slave_content`, `msg_slv`.`message_slave_sent_on`, `usr`.`user_name` AS `sender_name`
 FROM `sc_message_slave` AS `msg_slv` INNER JOIN `sc_user` AS `usr` ON `msg_slv`.`message_slave_sender_id` = `usr`.`user_id`
 WHERE `msg_slv`.`message_id` = 3 ORDER BY `msg_slv`.`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'))
                     ->joinInner(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);

    $this->_name = "sc_message_slave";
    $this->_primary = "message_slave_id";
    $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'))
                     ->joinInner(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);
4

1 回答 1

0

假设您正在使用一些 Zend_Db_Table 方法

//your code...

$unionSelect = $this->getAdapter()->select()->union(array($Query_1, $Query_2));
//no you can execute it
$rows = $this->getAdapter()->fetchAll($unionSelect);
于 2012-04-16T12:39:17.503 回答