如何在学说中编写联合查询?

Posted

技术标签:

【中文标题】如何在学说中编写联合查询?【英文标题】:How to write union query in doctrine? 【发布时间】:2013-10-07 10:09:20 【问题描述】:

我想在学说中使用联合,我搜索了很多但没有成功,这是我在sql中的联合查询,如何在学说中转换这个查询?

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

这是我的教义查询

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

我研究了很多,但没有取得任何成功,如果有人可以提供帮助,在此先感谢数百万。

【问题讨论】:

【参考方案1】:

我找到了解决办法

我们可以将这个查询与 RSM 一起使用,如下所示

“usman 基本上就是表名和类”

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

我们可以在没有 ORM 的情况下使用

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

【讨论】:

【参考方案2】:

对于联合查询有一些规则

(1) 所有 SELECT 语句具有相同的数据类型和相同的列数

在您的选择查询中具有不同的数据类型,没有列不匹配。

所以你找到了问题。

解决办法

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

【讨论】:

基本上我的 sql 查询非常好,它给了我 sql 的结果,我的问题是我怎样才能用学说语法编写这个查询。

以上是关于如何在学说中编写联合查询?的主要内容,如果未能解决你的问题,请参考以下文章

如何在联合查询的另一个选择中使用选择的结果?

你好!无论如何要在不使用联合的情况下编写类似的查询吗?

在 Laravel Eloquent 中,你如何在联合范围之外进行两个联合查询和一个选择?

sql联合查询UNION问题

如何在drupal 7中更改视图查询并添加联合本身

sql如何一对多联合查询