原则SQL查询顺序(按ASC NULL)最后

时间:2019-03-13 16:28:14

标签: symfony doctrine symfony-2.8

我使用的是 Doctrine 2.4 。然后,我尝试按优先级升序对我的广告系列列表进行排序,优先级为NULL,优先级最后为0。

SELECT "e"."First_name"
FROM public."Employee" as e
JOIN public."Manager_data" as m
ON "m"."ID" = "e"."Manager_id"
WHERE "Manager_id" = 4

您知道我该怎么做吗?

SELECT * FROM campaign ORDER BY priority IS NULL, priority = 0, priority ASC

2 个答案:

答案 0 :(得分:1)

order by case when priority is null then 1 else 0 end, priority

答案 1 :(得分:1)

我找到了解决方案,我做到了:

$query->addSelect('CASE WHEN c.priority IS NULL THEN 1 ELSE 0 END as HIDDEN priority_is_null');
$query->addSelect('CASE WHEN c.priority = 0 THEN 1 ELSE 0 END as HIDDEN priority_is_zero');
$query->addOrderBy('priority_is_null', 'ASC');
$query->addOrderBy('priority_is_zero', 'ASC');
$query->addOrderBy('c.priority', 'ASC');