在查询中使用别名的查询构建器

时间:2015-07-06 08:37:52

标签: mysql sql symfony doctrine-orm

是否可以使用这样的Query构建:

    $qb = $this->em->createQueryBuilder();

    $qb->select(
        'af.shortKey as wg, 
        af.id as afId', 
        'afl.name as afName', 
        'l.id as langId') // -> this one
        ->from('DatabaseBundle:ArticleFamily', 'af')
        ->leftJoin('af.articleFamilyLanguages', 'afl')
        ->leftJoin('afl.language', 'l')
        ->where('langId = :languageId') //-> this is causing the problem
        //if i use it like l.id = :languageId is working. But I don't want it like this.
        ->setParameter('languageId', $params['lang']);

我需要像这样使用它,因为我在网址中传递了一些参数而我无法使用l.id

如果我使用此查询,则会收到以下错误:

  

执行' SELECT a0_.short_key AS时发生异常   short_key0,a0_.id AS id1,a1_.name AS name2,l2_.id AS id3 FROM   article_family a0_ LEFT JOIN article_family_language a1_ ON a0_.id =   a1_.article_family_id LEFT JOIN语言l2_ ON a1_.language_id =   l2_.id WHERE id3 =?按名称排序2 ASC限制10 OFFSET 0'与params   [" 3&#34]:

     

SQLSTATE [42S22]:找不到列:1054未知列' id3'在'哪里   条款'

其中id3实际应为l2_.id

1 个答案:

答案 0 :(得分:2)

您无法在WHERE子句中使用字段别名。相反,您可以尝试使用HAVING

相关SO问题/答案:Can you use an alias in the WHERE clause in mysql?