Laravel 5.1 - 按两列排序无法按预期工作
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/34602229/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Laravel 5.1 - Order by two columns not working as intended
提问by jrgilman
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:
我有两列,季度数字和季度年份。Quarter_number 列存储 1 到 4 之间的值,而 quad_year 存储年份值。我希望数据像这样排序,例如:
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();
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?
不幸的是,它似乎没有按预期工作(我认为这类似于基数排序)。它最终只是按年份排序(或者最后一个 orderBy 语句是什么)。我是否必须为此编写自己的自定义基数排序?或者,还有更好的方法?
Thank you.
谢谢你。
回答by Alex
I think you have to use the quarter_year
first, like this:
我认为您必须使用第quarter_year
一个,如下所示:
$last_figures = QuarterData::where('company_id', '=', $company->id)
->orderBy('quarter_year')->orderBy('quarter_number')->get();