2016-06-21 13 views
0

Guten Morgen Jeder,Yii2 - Union von zwei Abfragen

Ich brauche ein wenig Hilfe. Ich habe 2 SQL-Abfragen, die ich vereinen muss. Dies ist meine erste Abfrage .. (sorry, wenn dies werde lange ist sein)

$query1 = new Query(); 
$query1->params([':category' => $category, ':ownerId' => $ownerId]); 
$query1->select('tmp.id, tmp.title , tmp.description, asst.thumbAssetUrl, b1.paramVal as duration, b2.paramVal as clips, asst.fileUrl as video') 
    ->from('listdb.baseData tmp') 
    ->innerJoin(
     'listdb.tag tag', 
     'tag.baseDataId = tmp.id and tag.tag = :category and tag.status = "active"' 
    ) 
    ->innerJoin(
     'listdb.baseParam b0', 
     'b0.baseDataId = tmp.id 
     and ((b0.paramName = "role" 
     and (b0.paramValue = "public")) 
     or ((select count(*) from listdb.baseParam temp 
     where temp.baseDataId = tmp.id and paramName = "role")=0)) 
     or (b0.paramName = "role" and b0.paramValue = "public" and tmp.owner = :ownerId)' 
    ) 
    ->leftJoin(
     'listdb.baseParam b1', 
     'b1.baseDataId = tmp.id and b1.paramName="duration" and b1.status = "active"' 
    ) 
    ->leftJoin(
     'listdb.baseParam b2', 
     'b2.baseDataId = tmp.id and b2.paramName="itemCount" and b2.status = "active"' 
    ) 
    ->leftJoin(
     'listdb.baseParam b3', 
     'b3.baseDataId = tmp.id and b3.paramName="previewUrl" and b3.status = "active"' 
    ) 
    ->leftJoin('assetdb.baseData asst', 'asst.id = b3.paramValue and asst.status = "active"') 
    ->where('tmp.status = "active" and tmp.application = "template" and tmp.role = "public"') 
    ->groupBy('tmp.id') 
    ->orderBy(['tmp.upTime' => SORT_DESC]); 

und meine zweite Abfrage ist dies ..

$query2 = new Query(); 
$query2->params([':category' => $category, ':ownerId' => $ownerId]); 
$query2->select('tmp.id, tmp.title , tmp.description, asst.thumbAssetUrl, b1.paramValue as duration, b2.paramValue as clips, asst.fileUrl as video') 
    ->from('listdb.baseData tmp') 
    ->innerJoin(
     'listdb.tag tag', 
     'tag.baseDataId = tmp.id and tag.tag = :category and tag.status = "active"' 
    ) 
    ->innerJoin(
     'listdb.baseParam b0', 
     'b0.baseDataId = tmp.id 
     and ((b0.paramName = "role" 
     and (b0.paramValue = "private" or b0.paramValue = "" and b0.paramValue != "public")) 
     or ((select count(*) from listdb.baseParam temp 
     where temp.baseDataId = tmp.id and paramName = "role")=0)) 
     or (b0.paramName = "role" and b0.paramValue = "public" and tmp.owner = :ownerId)' 
    ) 
    ->leftJoin(
     'listdb.baseParam b1', 
     'b1.baseDataId = tmp.id and b1.paramName="duration" and b1.status = "active"' 
    ) 
    ->leftJoin(
     'listdb.baseParam b2', 
     'b2.baseDataId = tmp.id and b2.paramName="item_count" and b2.status = "active"' 
    ) 
    ->leftJoin(
     'listdb.base_parambaseParameter b3', 
     'b3.baseDataId = tmp.id and b3.paramName="previewUrl" and b3.status = "active"' 
    ) 
    ->leftJoin('assetdb.baseData asst', 'asst.id = b3.paramValue and asst.status = "active"') 
    ->innerJoin('listdb.childRestricted cr', 'cr.baseDataId = tmp.id and cr.status = "active" and cr.owner = :ownerId') 
    ->where('tmp.status = "active" and tmp.application = "template" and tmp.role = "private"') 
    ->groupBy('tmp.id') 
    ->orderBy(['tmp.upTime' => SORT_DESC]); 

Ich habe versucht, diese Vereinigung mit

$query = $query2->union($query1, false); 

aber es scheint, dass es die zwei Abfragen nicht richtig vereinigt, weil dies die Ergebnisse verdoppelt. Was scheint das Problem zu sein. Danke im Voraus.

Antwort

1

starten:

$unionQuery = (new \yii\db\Query()) 
    ->from(['dummy_name' => $query1->union($query2)]); 

print_r($unionQuery); 

Weitere Einzelheiten über die amtliche Site

+0

Ich habe versucht, dies zu tun, aber es dauert nur ewig zu laden. Das gibt mir eine 504 Gateway Time-out. –

+0

Haben Sie die offizielle Website besucht? –

Verwandte Themen