教义返回错误与"eq",否与"in"



使用Symfony和Doctrine,我在"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();
}

eq替换in

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

您需要使用括号((用于子查询

->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 equal子查询
  • 用教义做一件事

相关内容

  • 没有找到相关文章

最新更新