Symfony组合了2个查询

时间:2016-04-13 14:38:05

标签: mysql symfony date doctrine-orm

我想从其他查询插入到当前查询获取最大日期。请看我的代码。

数据:

for inv_export = 0 return a2 & a1
for inv_export = 1 return a1

结果:

SELECT ..., i.inv_date, i.inv_export
FROM Sp.inventory AS i
...

WHERE i.inv_date IN (
    SELECT max(i.inv_date) from Sp.inventory WHERE i.inv_export = 1
);

mysql(正常工作):

$qb = $this
  ->createQueryBuilder('i')
  ->select('..., i.invDate')
  ...

  ->where('i.invExport = :export AND i.invDate = MAX(i.invDate)')
  ->setParameter('export', $export);

我尝试将其转换为symfony:

[HttpPost, ValidateInput(false)]

1 个答案:

答案 0 :(得分:4)

您可以使用以下子查询:

$sub = $this->createQueryBuilder('i2');
$sub->select("max(i2.inv_date)");
$sub->where("i.invExport= i2.invExport");


$qb = $this
  ->createQueryBuilder('i')
  ->select('..., i.invDate')
  ...
   ->where('i.invDate = ( '.$sub->getDQL().' )');
  ->andWhere('i.invExport = :export')
  ->setParameter('export', $export);

希望这个帮助