繁体   English   中英

创建3级子查询Propel

[英]Creating 3 level subquery Propel

嗨,大家好,我想问一下是否有人知道如何使用3级子查询在propel中创建选择语句...最初,我有这个查询,它的工作原理很好,但我希望它更像propel类型...谁能帮我这个?

这是我的查询

select c.*, count(d.id) as like_count from (
    select a.*, count(b.id) as points_count
    from (
        select * 
        from reviews 
        where user_id ='3') a 
    left join points as b on (a.id = b.type_id) 
    where b.type='review'
    group by a.id 
    order by a.created desc) c left join `like` d on (c.id = d.type_id) group by c.id

这是我到目前为止的内容,但是仅从2级子查询中,输出查询不正确

$review = ReviewsQuery::create()->filterByUserId($user_id);
$points = PointsQuery::create('b')
        ->withColumn("COUNT(b.Id)", 'points_count')
        ->addSelectQuery($review, 'a', true)
        ->toString(); // This is just to check what will be the query output

谁能帮我这个忙

我以前没有做过您需要做的事情,并且总是假设当我需要运行诸如您的复杂查询时,我只会让Propel运行自定义查询。

这可能不是执行此操作的适当方法,但是我想指出一下,以防万一您没有看到它。 在此页面的大约一半处,都有关于如何执行此操作的说明: http : //propelorm.org/documentation/03-basic-crud.html

您可以通过以下方法来辅助子查询:

__1级

$c1 = new ReviewsQuery();//First query
$c1->addSelfSelectColumns();//implement all columns
$c1->filterByUserId(3);//the where clause --where user_id ='3'--

__等级2

$s1 = new ReviewsQuery();//Second Level 
$s1->addSelectQuery($c1,"a"); // Implements first query
$s1->addAlias("b",PointsTableMap::COL_TYPE_ID); //prepare Join
$s1->addJoin("a.Id",PointsTableMap::alias("b",PointsTableMap::COL_TYPE_ID))//Add the join
$s1->where(PointsTableMap::alias("b",PointsTableMap::COL_TYPE)."=?",'review')//filter by -- where b.type='review' --

(...分组...选择...等...)

__等级3

$c3 = new ReviewsQuery();//The high level Query
$c3->addSelectQuery($s1,"c");//adding a subquery as "c" 
$c3->addJoin... //continue as a normal Query Object

注意名称空间... Propel2中存在一些错误

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM