我想在Symfony 2.3中使用Doctrine来运行这个查询。但是Doctrine似乎不理解CASE语句。有人能帮上忙吗?提前谢谢你!
SELECT max(id) id, name
FROM cards
WHERE name like '%John%'
GROUP BY name
ORDER BY CASE WHEN name like 'John %' THEN 0
WHEN name like 'John%' THEN 1
WHEN name like '% John%' THEN 2
ELSE 3
END, name发布于 2013-12-11 06:02:03
案例是特定于供应商的,并不是原生的原则支持。
如果结果很小,我的建议是提取整个结果集,然后对数组进行排序。
如果结果集太大,您应该编写一个本机查询并对实体进行消隐处理。有关这方面的更多信息,请参阅Doctrine Documentation on Native SQL。这看起来很可怕,但一旦你浏览了一个例子就会明白。
作为最后的手段,您可以绕过原则,使用低级原生SQL。详情请参见this post。
我知道Doctrine Extensions has an IfElse function可能行得通,但我没听过多少成功的故事。
发布于 2019-08-09 19:11:35
我也遇到过类似的问题,我不得不在结果的顶部加上一些数字前缀。所以我是这样决定的:
$qb = $this->createQueryBuilder('numberPrefix');
$qb
->select('country.code','numberPrefix.prefix')
->addSelect('
(CASE WHEN country.code = :firstCountryCode THEN 1
WHEN country.code = :secondCountryCode THEN 2
WHEN country.code = :thirdCountryCode THEN 3
WHEN country.code = :fourthCountryCode THEN 4
ELSE 5 END) AS HIDDEN ORD')
->innerJoin('numberPrefix.country','country')
->orderBy('ORD, country.id')
->setParameters(
[
'firstCountryCode' => $firstCountryCode,
'secondCountryCode' => $secondCountryCode,
'thirdCountryCode' => $thirdCountryCode,
'fourthCountryCode' => $fourthCountryCode,
]
);发布于 2018-08-07 01:01:00
当按关系表或本地列排序(如果不存在关系)时,此命令将为我做这项工作:
$doctrineQuery->add('orderBy', '(CASE WHEN COUNT(relation_table.uid)>0 THEN relation_table.price ELSE current_table.generic_price END) ASC');https://stackoverflow.com/questions/20488863
复制相似问题