Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Doctrine SQL Query Order By ASC NULL Last

I use Doctrine 2.4. And I try to order my list of campaign by priority ascending with priority NULL and priority to 0 at the end.

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

Have you got an idea how I can do this?

$query->addOrderBy('c.priority', 'ASC');
like image 435
pirmax Avatar asked Sep 06 '25 03:09

pirmax


2 Answers

I found the solution, I did that:

$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');
like image 65
pirmax Avatar answered Sep 10 '25 23:09

pirmax


order by case when priority is null then 1 else 0 end, priority
like image 44
Lulceltech Avatar answered Sep 10 '25 23:09

Lulceltech