简体   繁体   English

在查询中使用别名的查询生成器

[英]Query builder using alias name inside a query

Is it possible to have a Query build like this: 是否有可能这样查询构建:

    $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']);

I need to use it like this because I am passing some parameters in the url and I cannot use l.id 我需要这样使用它,因为我在url中传递了一些参数,而我不能使用l.id

If I am using this query I am getting the following error: 如果我使用此查询,则会收到以下错误:

An exception occurred while executing '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 language l2_ ON a1_.language_id = l2_.id WHERE id3 = ? 执行'SELECT a0_.short_key AS short_key0,a0_.id AS id1,a1_.name AS name2,l2_.id AS id3时发生异常来自article_family a0_ LEFT JOIN article_family_language a1_ ON a0_.id = a1_.article_family_id LEFT JOIN语言l2 a1_.language_id = l2_.id,其中id3 =吗? ORDER BY name2 ASC LIMIT 10 OFFSET 0' with params ["3"]: 带有参数[“ 3”]的ORDER BY name2 ASC LIMIT 10 OFFSET 0':

SQLSTATE[42S22]: Column not found: 1054 Unknown column 'id3' in 'where clause' SQLSTATE [42S22]:找不到列:1054“ where子句”中的未知列“ id3”

Where id3 should be actually l2_.id id3应该实际上是l2_.id

You cannot using field alias in WHERE clause. 您不能在WHERE子句中使用字段别名。 Instead, you could try with HAVING . 相反,您可以尝试使用HAVING

Related SO question/answer: Can you use an alias in the WHERE clause in mysql? 相关的SO问题/答案: 可以在mysql的WHERE子句中使用别名吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM