无法将SELECT MAX CASE与doctrine一起使用

时间:2017-07-28 08:02:38

标签: sql postgresql symfony doctrine-orm dql

我尝试使用max创建DQL请求(案例...) 我正在使用PostgreSQL

这里有一个表例子:(键值是随机的)

[------TABLE------]
[TYPE |KEY|VALUE  ]
[SWEET|843|WAFFLE ]
[SWEET|258|NUTELLA]

这就是我想要的:

[TYPE |DESSERT|SAUCE  ]
[SWEET|GAUFRE |NUTELLA]

这就是我正在完成的(工作)SQL:

select 
    type,
    max(case when key=843 then valeur end) as dessert,
    max(case when key=258 then valeur end) as sauce
from
    TABLE
group by type

但是,我无法将此SQL查询转换为DQL,这是我绑定的(不工作):

$qb = $this->createQueryBuilder('table')
->select('table.type')
->addSelect('max(case when table.key=843 then table.value end) as dessert')
->addSelect('max(case when table.key=258 then table.value end) as sauce')
->addGroupBy('table.type')

这是我的错误:

 [Syntax Error] Error: Unexpected ')' 

我尝试使用和不使用max,并且生成的DQL是正确的。 你能帮我吗?非常感谢你们!

1 个答案:

答案 0 :(得分:0)

解决方案是在子句:

的情况下添加ELSE
$qb = $this->createQueryBuilder('table')
->select('table.type')
->addSelect('max(case when table.key=843 then table.value else null end) as dessert')
->addSelect('max(case when table.key=258 then table.value else null end) as sauce')
->addGroupBy('table.type')

度过愉快的一天

相关问题