mysql - Laravel 订购当前到结束日期
问题描述
orderBy
只有在日期current date
范围内/之间starts_at
,我如何才能在 laravel/mysql 中ends_at
约会?
例如:
// Suppose Current Date: 2018-10-05
- Example A
= Starts At: NULL, Ends At: NULL, Created At: 2018-10-05
- Example B
= Starts At: NULL, Ends At: NULL, Created At: 2018-10-04
- Example C
= Starts At: 2018-10-01, Ends At: 2018-10-03, Created At: 2018-10-03
- Example D
= Starts At: 2018-10-03, Ends At: 2018-10-05, Created At: 2018-10-02
- Example E
= Starts At: 2018-10-05, Ends At: 2018-10-07, Created At: 2018-10-01
- Example F
= Starts At: 2018-10-07, Ends At: 2018-10-09, Created At: 2018-09-30
我当前的查询是(列是starts_at
和ends_at
):
Example::orderByRaw('if(isnull(starts_at) >= curdate() and isnull(ends_at) <= curdate(), starts_at, created_at) desc');
结果:
- Example A
- Example B
- Example C
- Example D
- Example E
- Example F
预期结果:
- Example E // First because of starts at 2018-10-05 until 2018-10-07
- Example A --
- Example B |_ // Same order based expired/null
- Example C |
- Example D --
- Example F // Still last because starts at is in the future
这是我的sqlfiddle。
解决方案
尝试这个:
Example::orderBy('ends_at','desc')->latest()->get();
推荐阅读
- php - 获取同一id不同表的数据
- r - windows中donsnow包中的makeCluster无限期挂起
- svg - SVG - 如何将变换矩阵应用于径向渐变?
- python - python 3.6 中 print("""Hello World's"s""""") 的输出
- file - 种子客户端如何在不将整个文件加载到 RAM 的情况下播种种子?
- asp.net - 如何在 asp.net core web api 中使用 jwt 令牌以仅读取用户信息而无需任何验证
- javascript - 如何在 Redux 中集成 FileReader?
- vba - 查找平均输入框 VBA
- php - 成功发送邮件时发送邮件出错
- python - 如何在使用 Scikit-learn 实现逻辑回归时调整阈值?