php 查某记录 的排行,php – Symfony Doctrine查询按子查询计算组后的排名

我正在使用Symfony 1.4和Doctrine 1.2 ORM构建应用程序.我对ORM的学说很新,并且我已经掌握了它,但我无法解决这个问题.

我有一个用户分数表(mbScoreByGenre),其中一个用户ID可以有一个parent_genre的多个用户分数记录.即 – 很多甚至很多

我的目标是根据给定parent_genre_id和user_id的累积分数查找特定用户的排名.我的排名算法使用子查询,我在构建一个有效的学说查询时遇到了很多麻烦.

这是我对mbScoreByGenre的学说模式

mbScoreByGenre:

actAs:

Timestampable: ~

columns:

id: { type: integer, primary: true, autoincrement: true }

user_id: { type: integer, notnull: true }

genre_id: { type: integer, notnull: true }

parent_genre_id: { type: integer, notnull: true }

score: { type: float, notnull: true, default: 0 }

A.首先我尝试做这样的事情:

$q = Doctrine_Query::create()

->select('((SELECT COUNT(1) AS num

FROM

(SELECT SUM(mbScoreByGenre.score)

WHERE SUM(mbScoreByGenre.score) > SUM(s.score)

AND mbScoreByGenre.parent_genre_id = '.$genre['parent_id'].'

AND s.parent_genre_id = '.$genre['parent_id'].'

GROUP BY mbScoreByGenre.user_id

) + 1) AS rank')

->from('mbScoreByGenre s')

->where('s.user_id = ?', array($user_id))

->groupBy('s.user_id')

->orderBy('rank');

but I got the following error Fatal error: Maximum function nesting level of ‘100’ reached, aborting! in \lib\vendor\symfony-1.4.14\lib\plugins\sfDoctrinePlugin\lib\vendor\doctrine\Doctrine\Query\Tokenizer.php on line 303. I don’t understand how to build the subquery so that it works.

B.所以我改变并尝试了不同的方法

$q = new Doctrine_RawSql();

$q ->addComponent('s', 'mbScoreByGenre')

->select('COUNT({*}) AS {rank}')

->from('(SELECT SUM(s.score) AS total_score

FROM mb_score_by_genre s

WHERE s.parent_genre_id = '.$genre['parent_id'].'

GROUP BY s.user_id)

')

->where('total_score >= (

SELECT SUM(s.score)

FROM mb_score_by_genre s

WHERE s.parent_genre_id = '.$genre['parent_id'].'

AND s.user_id = '.$user_id.'

GROUP BY s.user_id

)');

但是我收到了这个错误:Sql查询中的所有选定字段必须是格式tableAlias.fieldName.我使用Doctrine_RawSql的原因是我读到,doctrine 1.2不支持From中的子查询.对于这种方法,我无法弄清楚如何引用tableAlias.fieldName格式的“total_score”列.我是否必须添加一个空白组件,该组件引用为“total_score”返回的子查询表?

C.最后,我尝试将子查询作为学说查询运行,并通过计算查询返回的学说对象行来计算排名.

$q = Doctrine_Query::create()

->select('SUM(s.score)')

->from('mbScoreByGenre s')

->where('s.parent_genre_id = ?', $genre['parent_id'])

->andWhere('SUM(s.score) > (

SELECT SUM(p.score)

FROM mbScoreByGenre p

WHERE p.parent_genre_id = '.$genre['parent_id'].'

AND p.user_id = '.$user_id.'

GROUP BY p.user_id

)')

->groupBy('s.user_id');

$result = $q->execute();

但它给了我错误:

SQLSTATE[HY000]: General error: 1111 Invalid use of group function. Is it because groupBy(‘s.user_id’) and GROUP BY p.user_id, both p and s refer to the same model?

我已经在网上搜索了大量的答案,但我似乎找不到3种方法中任何一种的答案.

任何帮助都会很棒.欣赏它.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值