2016-06-21 5 views
0

おはようございます皆さん、Yii2 - 2つのクエリの連合

少し助けが必要です。私は団結する必要がある2つのSQLクエリがあります。これが(すみません、このつもり長くなる場合)私の最初のクエリ..

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

であると私の2番目のクエリはこれです..私は、この組合

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

を使用してみました

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

2つのクエリを正しく結合することはできません。なぜなら、結果が2倍になるからです。これに問題があるようです。前もって感謝します。

答えて

1

このお試しください:公式Site

+0

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

もっと詳細を私はこれをやってみましたが、それは単に負荷に永遠にかかります。私に504のゲートウェイタイムアウトが与えられます。 –

+0

あなたは公式サイトを訪問してください? –

関連する問題