Pregunta

What I want to achieve is something like this:

SELECT * FROM users ORDER BY (IF ranking IS NULL, 9999, ranking) ASC

So, I need an if in my orderby. But it seems that user defined functions (I created one named ComplexIf) are not working in OrderBy.

->addOrderBy('ComplexIf(u.ranking IS NULL, 9999, u.ranking )', 'asc')

What am I doing wrong? How can I achieve this?

¿Fue útil?

Solución

Thanks SO MUCH to Nic, I finally found the solution with a CASE instead of an IF!

$query = $this
            ->createQueryBuilder( 'a' )
            ->select('a')
            ->add('from', 'path\to\whatever\table a')
            ->addSelect('CASE WHEN a.ranking IS NULL THEN 9999 ELSE a.ranking END as HIDDEN ORD')
            ->where( 'a.deleted IS NULL' )
            ->orderBy(  'ORD', 'asc' )
            ->getQuery()
            ;     
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top