【问题标题】:Laravel orderBy with column valueLaravel orderBy 与列值
【发布时间】:2018-01-19 08:41:31
【问题描述】:

这是我目前拥有的代码:

$programmefundings = Programmefunding::where('status', '!=', 'draft')->orderByRaw("FIELD(status , 'open', 'announced', 'delayed', 'closed') ASC")->orderBy('date_start', 'desc')->get();

因此,它获得了我所有的计划资金,除了状态为 'draft' 的那些,将它们从 'open' 重新排序为 'close' 并按 'date_start' 排列它们。

我需要在 'asc' 顺序中的另一列 'announcement_date' 中重新排序具有“已宣布”状态的 programfundings,而不会影响 'open' '已关闭' 结构,并且不影响任何其他不具有 'announced' 状态值的节目发现。

这可能吗?有人知道怎么做吗?

谢谢!

【问题讨论】:

    标签: php jquery laravel eloquent sql-order-by


    【解决方案1】:

    您应该可以使用IF 语句来做到这一点。

    $programmefundings = Programmefunding::where('status', '!=', 'draft')
        ->orderByRaw("FIELD(status , 'open', 'announced', 'delayed', 'closed') ASC")
        ->orderByRaw("IF(status = 'announced', accouncement_date, date_start) DESC")
        ->get();
    

    【讨论】:

      【解决方案2】:

      我认为您想要做的是这样的事情。它将首先按公告日期对所有状态为公告的项目进行排序,然后按日期开始对其他所有项目进行排序。

      $programmefundings = Programmefunding::where('status', '!=', 'draft')
          ->orderByRaw("case when status = 'announced' then announcement_date end asc")
          ->orderBy('date_start', 'desc')->get();
      

      【讨论】:

        猜你喜欢
        • 2018-10-24
        • 1970-01-01
        • 2019-05-14
        • 2022-01-16
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多