原则2由ASC和Null值排列在最后 [英] Doctrine 2 Order By ASC and Null values in last

查看:112
本文介绍了原则2由ASC和Null值排列在最后的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试获取结果,我需要按升序排序。但是一些值将为空/空,我需要在最后一次排序从0 1 2开始,然后是空值。

I am trying to fetch result which and I need to sort in ascending order. But some values would be null/empty, I need the to be in last while sorting starts from 0 1 2 and then null value.

我尝试了SortableNullsWalker,但没有帮助。我正在排序的值不是列,它是正在排序的两个值的乘法,这就是为什么我认为SortableNullsWalker不起作用。任何帮助请

I tried SortableNullsWalker but it did not help. The value I am sorting is not column, its a multiplication of two values which is being sorted, thats why I think SortableNullsWalker did not work. Any help please

$dql = 'SELECT (column1 * column2) as distance FROM table)

                ORDER BY distance ASC ';

        $dq = $this->getEntityManager()->createQuery($dql);

结果来自'','',0,1,2.334 ,....

但我试图得到它: 0,1,2.334,.. ....,'',''

推荐答案

解决方案

我不得不使用与距离相同的隐藏变量,并在其前添加 minus( - )并在 DESC订单

I had to use a hidden variable with same value as of distance and add minus(-) before it and Order the result by new hidden variable in DESC order

$dql = 'SELECT (column1 * column2) as distance,
        -(column1 * column2) as HIDDEN distance1 
        FROM table
        ORDER BY distance1 DESC';

        $dq = $this->getEntityManager()->createQuery($dql);

这篇关于原则2由ASC和Null值排列在最后的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆