2014-09-02 82 views
0
SELECT * FROM tab1 union select * from tab2 union select * from tab3 union select * from tab4; 

我想把這個查詢轉換成ZF2查詢選擇Zend框架2查詢四個表

我試圖

$sql->select() 
    ->from(array('a' => 'tab1', 'b' => 'tab2', 'c' => 'tab3', 'd' => 'tab4')); 

但查詢沒有工作,我想聯盟所有四個表

+1

在ZF2,你'合併()'做了'union'。檢查它。 – 2014-09-02 09:07:33

回答

0

有沒有一種直接的方式來獲得使用combine()形成所需的SQL查詢。

但是有一項工作要得到相同的結果集。

試試這個 -

$select1 = $sql->select('tab1'); 
$select2 = $sql->select('tab2'); 
$select1->combine($select2); 

$select3 = $sql->select('tab3'); 

$selectall3 = $sql->select(); 
$selectall3->from(array('sel1and2' => $select1)); 
$selectall3->combine($select3); 

$select4 = $sql->select('tab4'); 

$selectall4 = $sql->select(); 
$selectall4->from(array('sel1and2and3' => $selectall3)); 
$selectall4->combine($select4); 

現在使用$selectall4執行語句。

這會給你同樣的結果你想要查詢 -

SELECT * FROM tab1 union select * from tab2 union select * from tab3 union select * from tab4;