在zf2查询中使用union选择五个以上的表

时间:2014-09-01 13:44:41

标签: mysql zend-framework2

select union in zf2 query代码正在使用union工作三个表但我需要联合超过五(5)个表...帮我解决这个问题。

$dbAdapter = $this->adapter;
        $sql = new Sql($dbAdapter);
        $select1 = $sql->select();
    $select1->from('java');
    $select2 = $sql->select();
    $select2->from('dotnet');
    //union of two first selects
    $select1->combine ( $select2 );


    $select3 = $sql->select();
    $select3->from('android');
$select4 = $sql->select();
    $select4->from('network');
    $select5 = $sql->select();
    $select5->from('dmining');
    $select3->combine ( $select4 );
    //Final select

    $select = $sql->select();
    $select->from(array('sel1and2' => $select1, 'sel3and4' => $select3));


     $sQuery = $select->combine ( $select5 );

1 个答案:

答案 0 :(得分:1)

试试这个 -

$select1 = $sql->select('java');
$select2 = $sql->select('dotnet');
$select1->combine($select2);

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

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

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

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

$select5 = $sql->select('dmining');

$selectall5 = $sql->select();
$selectall5->from(array('sel1and2and3and4' => $selectall4));
$selectall5->combine($select5);

现在使用$selectall5执行语句。

提供与执行以下语句相同的结果 -

SELECT * FROM java 
UNION SELECT * from dotnet 
UNION SELECT * from android 
UNION SELECT * from network;
UNION SELECT * from dmining;

我希望它有所帮助。