2013-10-01 45 views
8

我想在教條中使用聯合,我搜索了很多,但沒有得到任何成功,這是我在sql中的聯合查詢,如何將這個查詢轉換爲教條?如何在教義中編寫聯合查詢?

select * from (select orderid,tutorialId,points,allow_multiple,question,answer1,image1,correct1,answer2,image2,correct2,answer3,image3,correct3,answer4,image4,correct4,answer5,image5,correct5,'1' as istest,'' as content,'' as media,'' as media_type_id from tutorial_test 

union 

select orderid,tutorialId,'0' as istest,content,media,media_type_id,'' as points,'' as allow_multiple,'' as question,'' as answer1,'' as image1,'' as correct1,'' as answer2,'' as image2,'' as correct2,'' as answer3,'' as image3,'' as correct3,'' as answer4,'' as image4,'' as correct4,'' as answer5,'' as image5,'' as correct5 from tutorial_elements) a where a. tutorialId = 1 order by orderid asc 

,這一個是我的教訓查詢

$query = "SELECT * FROM(SELECT 
        tt.tutorialid 
       FROM 
        TutorialTest tt 
       UNION 
       SELECT te.tutorialid) tte 
       WHERE tte.tutorialid = 1  

    "; 
    $qb  = $this->Doctrine->createQuery($query); 
    $tutorial_test = $qb->getResult(); 

我研究了很多,但沒有得到任何的成功,如果任何人能幫助,謝謝萬元提前FOT這一點。

回答

8

好吧,我已經找到了解決

我們可以利用這個查詢與RSM如下

「烏斯曼基本上是表名和類」

$rsm = new ResultSetMapping(); 
    $rsm->addEntityResult('Usmans', 'u'); 
    $rsm->addFieldResult('u', 'orderid', 'orderid'); 
    $rsm->addFieldResult('u', 'tutorialId', 'tutorialid'); 
    $rsm->addFieldResult('u', 'points', 'points'); 

    $query = $this->Doctrine->createNativeQuery('SELECT * FROM usman', $rsm); 
    $tutorial_tests = $query->getResult(); 

我們可以使用沒有ORM作爲

$testQuery = " 
    select * from (
      select orderid, 
       tutorialId, 
       points, 
       allow_multiple, 
       question, 
       answer1, 
       image1, 
       correct1, 
       answer2, 
       image2, 
       correct2, 
       answer3, 
       image3, 
       correct3, 
       answer4, 
       image4, 
       correct4, 
       answer5, 
       image5, 
       correct5, 
       '1' as istest, 
       '' as content, 
       '' as media, 
       '' as media_type_id 
      from tutorial_test 

      union 

      select orderid, 
       tutorialId, 
       '0' as istest, 
       content, 
       media, 
       media_type_id, 
       '' as points, 
       '' as allow_multiple, 
       '' as question, 
       '' as answer1, 
       '' as image1, 
       '' as correct1, 
       '' as answer2, 
       '' as image2, 
       '' as correct2, 
       '' as answer3, 
       '' as image3, 
       '' as correct3, 
       '' as answer4, 
       '' as image4, 
       '' as correct4, 
       '' as answer5, 
       '' as image5, 
       '' as correct5 
      from tutorial_elements 
     ) a 
     where a. tutorialId = $tutorial_id 
     order by orderid asc 
"; 

$resultSets = $this->Doctrine->getConnection()->fetchAll($testQuery); 
-3

對於聯合查詢有一定的規則

(1)所有SELECT語句具有相同的數據類型和列

在您選擇查詢的不一樣了不同的數據類型,不列都無法比擬的。

所以你發現了問題。

這裏是解決

select orderid, tutorialId, points, allow_multiple, question, answer1, image1, correct1, answer2, image2, correct2, answer3, image3, correct3, answer4, image4, correct4, answer5, image5, correct5,'1' as istest,'' as content,'' as media,'' as media_type_id 
from tutorial_test 

union 

select orderid, tutorialId,'0' as istest, content, media, media_type_id,'' as points,'' as allow_multiple,'' as question,'' as answer1,'' as image1,'' as correct1,'' as answer2,'' as image2,'' as correct2,'' as answer3,'' as image3,'' as correct3,'' as answer4,'' as image4,'' as correct4,'' as answer5,'' as image5,'' as correct5, '1' as istest,'' as content,'' as media,'' as media_type_id 
from tutorial_elements 
where a. tutorialId = 1 
order by orderid asc 
+0

基本上我的sql查詢是完全正常的,它給了我結果在sql中,我的問題是,我怎麼能寫在教條語法這個查詢。 – usii