首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Yii2 -两个查询的联合

Yii2 -两个查询的联合
EN

Stack Overflow用户
提问于 2016-06-21 12:06:31
回答 1查看 1.7K关注 0票数 0

大家早上好,

我需要一点帮助。我有两个SQL查询需要联合。这是我的第一个问题..(抱歉,如果这会很长的话)

代码语言:javascript
复制
$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]);

我的第二个问题是..

代码语言:javascript
复制
$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]);

我试着用这个联盟

代码语言:javascript
复制
$query = $query2->union($query1, false);

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

EN

回答 1

Stack Overflow用户

发布于 2016-06-21 14:40:11

试试这个:

代码语言:javascript
复制
 $unionQuery = (new \yii\db\Query())
    ->from(['dummy_name' => $query1->union($query2)]);

 print_r($unionQuery);

有关官方Site的更多详细信息

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/37935402

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档