(laravel)两个ORDER BY子句和一个LIMIT

时间:2017-04-17 01:48:51

标签: php sql laravel

这是一些排序:

  1. 我想根据"观看"。
  2. 列出20个最佳帖子
  3. 然后,根据" created_at"。
  4. 对其进行排序

    怎么做?

    继承我当前的代码(哪个有效,但没有按" created_at&#34排序):

    $this->data['today_post'] = 
    Posts::orderBy('views', 'desc')
    ->orderBy('created_at', 'desc')
    ->whereRaw('created_at >= NOW() - INTERVAL 1 DAY')
    ->limit(20)->get();
    

3 个答案:

答案 0 :(得分:0)

你可以写这个。希望这能解决您的问题

META-INF/
META-INF/MANIFEST.MF
org/
org/apache/
org/apache/zeppelin/
org/apache/zeppelin/markdown/
interpreter-setting.json
META-INF/DEPENDENCIES
META-INF/LICENSE
META-INF/NOTICE
org/apache/zeppelin/markdown/Markdown$1.class
org/apache/zeppelin/markdown/Markdown$MarkdownParserType$1.class
org/apache/zeppelin/markdown/Markdown$MarkdownParserType$2.class
org/apache/zeppelin/markdown/Markdown$MarkdownParserType.class
org/apache/zeppelin/markdown/Markdown.class
org/apache/zeppelin/markdown/Markdown4jParser.class
org/apache/zeppelin/markdown/MarkdownParser.class
org/apache/zeppelin/markdown/ParamVar.class
org/apache/zeppelin/markdown/PegdownParser.class
org/apache/zeppelin/markdown/PegdownWebSequencelPlugin.class
org/apache/zeppelin/markdown/PegdownYumlPlugin.class
META-INF/maven/
META-INF/maven/org.apache.zeppelin/
META-INF/maven/org.apache.zeppelin/zeppelin-markdown/
META-INF/maven/org.apache.zeppelin/zeppelin-markdown/pom.xml
META-INF/maven/org.apache.zeppelin/zeppelin-markdown/pom.properties

同时在控制器的开头添加$this->data['today_post'] = Posts::orderBy('views', 'desc') ->orderBy('created_at', 'desc') ->whereDate('created_at', Carbon::today()->toDateString()) ->limit(20) ->get();

答案 1 :(得分:0)

首先从数据库中获取一个集合

$x = Posts::whereRaw('created_at >= NOW() - INTERVAL 1 DAY')->orderBy('views', 'desc')->limit(20)->get();

然后对集合进行排序

降序:

$this->data['today_post'] = $x->sortByDesc('created_at'); 

升序:

$this->data['today_post'] = $x->sortBy('created_at'); 

答案 2 :(得分:0)

您可以简单地对结果集合进行排序。保留您的代码,添加以下内容:

$this->data['today_post'] = $this->data['today_post']->sortBy('created_at');