gpt4 book ai didi

php - Doctrine 从子查询中选择

转载 作者:行者123 更新时间:2023-11-29 02:31:30 24 4
gpt4 key购买 nike

我正在尝试通过 Doctrine 运行以下查询:

$sql = "SELECT league_id, sum(num_fans) AS total_fans FROM ("
."SELECT COUNT(*) as num_fans, t.id AS team_id, l.id AS league_id FROM fan_link fl "
."JOIN team t ON fl.team_id = t.id JOIN League l ON t.league_id = l.id GROUP BY fl.team_id"
.") a GROUP BY league_id LIMIT 0, 3";

$results = $this->em->createQuery($sql)->getScalarResult();

查询在 PHPMyAdmin 中运行良好,但是当我尝试在 doctrine 中运行它时,我得到了这个错误:

Doctrine\ORM\Query\QueryException [ 0 ]: 
[Semantical Error] line 0, col 51 near '(SELECT COUNT(*)':
Error: Class '(' is not defined.

括号 ( 是 Doctrine 查询中的保留字符吗?我在其他地方的 where 子句中有子选择,但它在 from 子句不想工作。我做错了什么?

更新:
我刚刚尝试使用查询生成器并得到了同样的错误。代码:

$qb = $this->em->createQueryBuilder();
$leagueQuery = $qb->select('league_id, sum(num_fans) AS total_fans')
->from("(SELECT count(*) as num_fans, t.id AS team_id, l.id AS league_id "
."FROM fan_link fl "
."JOIN team t ON fl.team_id = t.id "
."JOIN League l ON t.league_id = l.id GROUP BY fl.team_id)",
'a')
->groupBy("league_id")
->orderBy("total_fans", "DESC")
->setMaxResults(3)
->getQuery();

$results = $leagueQuery->getArrayResult();

最佳答案

最终使用了这个:

$sql = "SELECT league_id, sum(num_fans) AS total_fans FROM "
."(SELECT COUNT(*) as num_fans, t.id AS team_id, l.id AS league_id FROM fan_link fl "
."JOIN team t ON fl.team_id = t.id JOIN League l ON t.league_id = l.id GROUP BY fl.team_id"
.") a GROUP BY league_id LIMIT 0, 3";
$q = $this->em->getConnection();
$league_results = $q->fetchAll($sql);

关于php - Doctrine 从子查询中选择,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12554202/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com