2013-10-01 7 views
8

Ich möchte Union in der Lehre verwenden, ich habe viel gesucht, aber keinen Erfolg, das ist meine Union Abfrage in SQL, wie diese Abfrage in Doktrin konvertieren?Wie schreibe ich Union-Anfrage in der Lehre?

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 

und dies ist meine Lehre Abfrage

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

ich eine Menge recherchiert, aber nicht bekam keinen Erfolg, wenn jemand, Millionen Dank im Voraus fot die helfen kann.

Antwort

8

Nun, ich gefunden habe, eine Lösung

Wir haben diese Abfrage mit RSM verwenden können wie folgt

"Usman ist im Grunde Tabellenname und Klasse"

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

UND wir können ohne ORM als

verwenden
$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

Für Unionsabfrage hat einige Regeln

(1) Alle SELECT-Anweisungen gleichen Datentyp und die gleiche Anzahl der Spalten

In Ihrer Auswahlabfrage haben verschiedenen Datentyp haben, wird keine der Spalten nicht überein.

Sie haben also gefunden Proble.

hier Lösung

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

grundsätzlich meine SQL-Abfrage ist völlig in Ordnung, und es gibt mir Ergebnis in SQL, meine Frage ist, dass wie kann ich diese Abfrage in Doktrin Syntax schreiben. – usii

Verwandte Themen