Yii2 - 两个查询联盟

时间:2016-06-21 04:06:31

标签: mysql yii2

大家早上好,

我需要一些帮助。我有两个需要联合的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]);

我的第二个问题就是这个..

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

我尝试使用这个联盟

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

但它似乎没有正确地将两个查询联合起来,因为这会导致结果加倍。这似乎是什么问题。提前谢谢。

1 个答案:

答案 0 :(得分:1)

试试这个:

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

 print_r($unionQuery);

关于官方Site

的更多详情