postgresql - 在 JOIN 中向 Doctrine Query Builder 添加排除项
问题描述
我在 Symfony 应用程序中使用 Doctrine Query Builder 构建了以下查询。
$qb->select('c')
->from('AppBundle:Course', 'c')
->join('AppBundle:Log', 'a', Expr\Join::WITH, $qb->expr()->eq('c.id', 'a.course'))
->where($qb->expr()->in('a.type', ':type'))
->andWhere($qb->expr()->between('a.time', ':start', ':end'))
->andWhere($qb->expr()->eq('c.status', ':status'))
->setParameter(':type', ['opened'])
->setParameter(':standardScratchScore', [74])
->setParameter(':status', Course::OPENED)
->setParameter(':start', $dateFrom->format('Y-m-d H:i:s'))
->setParameter(':end', $dateTo->format('Y-m-d H:i:s'))
;
在我的代码中,我遍历Course
s ,然后再次查询Log
表以检查是否不存在具有特定类型的条目Course
。log.type = 'log.sent-email'
有没有一种方法可以将本课程的排除合并到此初始查询中,而无需使用子选择之类的东西?
在循环中再次查询同一个表对我来说不是最理想的,NewRelic 认为它会损害我的应用程序的性能。
解决方案
好吧,您总是可以再加入一次来满足这一特定需求:
$qb->select('c')
->from('AppBundle:Course', 'c')
->join('AppBundle:Log', 'a', Expr\Join::WITH, $qb->expr()->eq('c.id', 'a.course'))
->leftjoin(
'AppBundle:Log',
'b',
Expr\Join::WITH,
$qb->expr()->andx(
$qb->expr()->eq('c.id', 'b.course'),
$qb->expr()->eq('b.type', 'log.sent-email')
))
) // join log a second time, with the type condition
->where($qb->expr()->in('a.type', ':type'))
->andWhere($qb->expr()->between('a.time', ':start', ':end'))
->andWhere($qb->expr()->eq('c.status', ':status'))
->andWhere($qb->expr()->isNull('b.type')) // Only select records where no log record is found
->setParameter(':type', ['opened'])
->setParameter(':standardScratchScore', [74])
->setParameter(':status', Course::OPENED)
->setParameter(':start', $dateFrom->format('Y-m-d H:i:s'))
->setParameter(':end', $dateTo->format('Y-m-d H:i:s'))
;
推荐阅读
- java - 如何为spring-boot oauth2 tokenstore中的每个请求获取一个新令牌
- git - 无法从我的 Git Repo 历史记录中删除大型 tarball(通过 git filter-branch)
- javascript - 从具有数组值的 json 文件中提取项目
- typescript - 如何将传入的 JSON 数据转换为具有动态数据的行和列的表格格式?
- android - 重命名应用程序和新应用程序图标 - 最佳实践是什么?
- powershell - 获取 WmiObject Win32_networkadapterconfiguration
- mongodb - MongoDB 合并展开的文档
- javascript - 无法读取未定义的属性“toLocaleString”
- c# - 传入 ViewDataDictionary 的模型项在 nopcommerce 4.2 中属于“Castle.Proxies.Model”类型
- css - SCSS 字体名称变成十六进制