尝试从Symfony2中的数据库获取列的总和时出错

时间:2014-11-26 21:04:37

标签: mysql symfony

我正在尝试在数据库列中获取多个值的总和:

                $goalsScored = $em->createQueryBuilder()
                ->select('sum(homeGoals)')
                ->from('LoginLoginBundle:Matchgame', 'mg')
                ->where("mg.homeTeam LIKE '" . $oneTeam->getTeamid()."'")
                ->getQuery()
                ->getSingleScalarResult();

我收到以下错误:

[Semantical Error] line 0, col 11 near 'homeGoals) FROM': Error: 'homeGoals' is not defined. 

homeGoals在我的数据库中定义,为什么我会收到此错误?是因为某些值为空吗?

1 个答案:

答案 0 :(得分:4)

您的选择中缺少表别名:

    $goalsScored = $em->createQueryBuilder()
    ->select('sum(mg.homeGoals)')
    ->from('LoginLoginBundle:Matchgame', 'mg')
    ->where("mg.homeTeam LIKE '" . $oneTeam->getTeamid()."'")
    ->getQuery()
    ->getSingleScalarResult();