Laravel 5.1-按两列排序无法正常工作 [英] Laravel 5.1 - Order by two columns not working as intended
问题描述
我有两列,Quarter_number和Quarter_year. Quarter_number列存储的值介于1到4之间,而Quarter_year列存储的是年份值.我希望对数据进行排序,例如:
I have two columns, quarter_number and quarter_year. The quarter_number column stores a value between 1 and 4, while quarter_year stores a year value. I want the data to be sorted like so for example:
ex: (quarter_number - quarter_year)
4 - 2015
3 - 2015
2 - 2015
1 - 2015
4 - 2014
3 - 2014
etc...
因此,这种说法似乎行得通:
Thus it seemed like this statement would work:
$last_figures = QuarterData::where('company_id', '=', $company->id)
->orderBy('quarter_number')->orderBy('quarter_year')->get();
$last_figures = QuarterData::where('company_id', '=', $company->id)
->orderBy('quarter_number')->orderBy('quarter_year')->get();
不幸的是,它似乎没有按预期工作(我认为这类似于基数排序).它最终仅按年份排序(或最后一个orderBy语句是什么).我是否需要为此编写自己的自定义基数排序?还是有更好的方法?
Unfortunately, it doesn't seem to work as intended (which I thought would be similar to a radix sort). It ends up just sorting by the year (or whatever to last orderBy statement is). Do I have to just program my own custom radix sort for this? Or is there a better way?
谢谢.
推荐答案
我认为您必须首先使用quarter_year
,如下所示:
I think you have to use the quarter_year
first, like this:
$last_figures = QuarterData::where('company_id', '=', $company->id)
->orderBy('quarter_year')->orderBy('quarter_number')->get();
这篇关于Laravel 5.1-按两列排序无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!