创建3级子查询Propel

时间:2013-11-08 10:22:01

标签: php mysql symfony propel

大家好我想问你们是否有人知道如何使用3级子查询在推进中创建一个select语句...最初我有这个查询并且它工作正常但我希望它更像推进类型......任何人都可以帮我这个吗?

这是我的查询

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

任何人都可以帮我这个吗

2 个答案:

答案 0 :(得分:0)

我之前没有做过你需要做的事情,并且总是假设我需要运行像你这样的复杂查询,我会让Propel运行自定义查询。

这可能不适合这样做,但我想我会指出它,以防它不是你见过的东西。关于如何在此页面上执行此操作的说明大约有一半:http://propelorm.org/documentation/03-basic-crud.html

答案 1 :(得分:0)

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

__第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中有一些错误正在工作