Php 按多列值排序,带原则
我正试图找到一种方法,使用symfony2和条令,根据两列中的最小值对数据进行排序,类似于Php 按多列值排序,带原则,php,sql,symfony,doctrine,Php,Sql,Symfony,Doctrine,我正试图找到一种方法,使用symfony2和条令,根据两列中的最小值对数据进行排序,类似于 $em=$this->getDoctrine()->getManager(); $test = $em->createQuery('select t from AcmeBundle:Test t where t.name is not null order by t.price1, t.price2')->getResult(); 问题是这样做首先按price1排序,然后按pri
$em=$this->getDoctrine()->getManager();
$test = $em->createQuery('select t from AcmeBundle:Test t where t.name is not null order by t.price1, t.price2')->getResult();
问题是这样做首先按price1
排序,然后按price2
排序
是否有一种方法可以使其按
price1
和price2
中的最小值进行排序,组合而不是单独排序 选择并按COLLMNS的最小值订购price
和price1
:
$test = $em->createQuery('select b, CASE WHEN (price1 > price) THEN price ELSE price1 END as order_value from AcmeBundle:Test t where t.name is not null order by order_value')->getResult();
不完全是,我需要它按最小值排序,它可以是price1或price2,在哪一个中并不重要。感谢快速重播,但当我这样做时,我得到:[语法错误]:错误:预期已知函数,得到'IF'Ok,所以让我们尝试
CASE when
而不是IF
。我更新了我的答案。这就成功了,唯一的问题是现在我得到的结果是一个多维数组,但实际上我需要的结果是一个对象数组。您可能知道有没有办法克服这种情况。请使用getArrayResult()
而不是getResult()
;)