使用“eq”返回错误,使用“in”返回no

cedebl8k  于 2021-06-18  发布在  Mysql
关注(0)|答案(1)|浏览(273)

对于symfony和条令,“eq”子查询有一个错误:
没关系,没有错误:

public function getForums()
{
    $qb = $this->createQueryBuilder('fc');

    return $qb
        ->innerJoin('fc.versions', 'fcv')
        ->innerJoin('fc.versions', 'fcvl', 'WITH', $qb->expr()->in(
            'fcvl.id',
            $this->_em->createQueryBuilder()
                ->select('MAX(v.id)')
                ->from(ForumCategoryVersion::class, 'v')
                ->where('v.forumCategory = fc')
                ->getDQL()
        ))
        ->select('fc, fcv')
        ->getQuery()
        ->getResult();
}

替换 ineq :

public function getForums()
{
    $qb = $this->createQueryBuilder('fc');

    return $qb
        ->innerJoin('fc.versions', 'fcv')
        ->innerJoin('fc.versions', 'fcvl', 'WITH', $qb->expr()->eq(
            'fcvl.id',
            $this->_em->createQueryBuilder()
                ->select('MAX(v.id)')
                ->from(ForumCategoryVersion::class, 'v')
                ->where('v.forumCategory = fc')
                ->getDQL()
        ))
        ->select('fc, fcv')
        ->getQuery()
        ->getResult();
}

我有一个错误:
[语法错误]第0行,第208列:错误:应为literal,得到'select'

pdkcd3nj

pdkcd3nj1#

您需要对子查询使用括号():

->innerJoin('fc.versions', 'fcvl', 'WITH', $qb->expr()->eq(
        'fcvl.id',
        '(' . $this->_em->createQueryBuilder()
            ->select('MAX(v.id)')
            ->from(ForumCategoryVersion::class, 'v')
            ->where('v.forumCategory = fc')
            ->getDQL() . ')'
    ))

参考文献
mysql等子查询
做一个有条理的调查

相关问题