按 Laravel 5.3 中两列的差值排序
Order by the difference of two columns in Laravel 5.3
我有一个 eloquent 查询,其中 orderBy 之一是两列的差异。
$mymodel = Level::where([['ColA', 5], ['ColB', 10], ['ColC', 7]])
->orderBy('ColA', 'Desc')
->orderBy('ColA' - 'ColB', 'Desc')
->orderBy('ColC', 'Desc')
->orderBy('ColD', 'Asc')
->pluck('userId')->toArray();
使用 sqlite 在 localhost 上使用完全相同的代码可以正常工作。但是在 MySQL 的生产中有以下错误
SQLSTATE[42S22]: Column not found: 1054 Unknown column '0' in 'order clause' (SQL: select `userId` from `levels` where (`ColA` = 5 and `ColB` = 10 and `ColC` = 7) order by `ColA` desc, `0` desc, `ColC` desc, `ColD` asc)
$model = Level::where($wheres)
->orderByRaw('(ColA - ColB) DESC')
->pluck('userId')
->toArray();
我有一个 eloquent 查询,其中 orderBy 之一是两列的差异。
$mymodel = Level::where([['ColA', 5], ['ColB', 10], ['ColC', 7]])
->orderBy('ColA', 'Desc')
->orderBy('ColA' - 'ColB', 'Desc')
->orderBy('ColC', 'Desc')
->orderBy('ColD', 'Asc')
->pluck('userId')->toArray();
使用 sqlite 在 localhost 上使用完全相同的代码可以正常工作。但是在 MySQL 的生产中有以下错误
SQLSTATE[42S22]: Column not found: 1054 Unknown column '0' in 'order clause' (SQL: select `userId` from `levels` where (`ColA` = 5 and `ColB` = 10 and `ColC` = 7) order by `ColA` desc, `0` desc, `ColC` desc, `ColD` asc)
$model = Level::where($wheres)
->orderByRaw('(ColA - ColB) DESC')
->pluck('userId')
->toArray();