我从Pragnesh Khalas收到此查询,有人可以将其转换为laravel查询吗?
SELECT * FROM Position ORDER BY
CASE WHEN position_value = 1 THEN created_at END ASC,
CASE WHEN position_value = 2 THEN created_at END DESC
我已经这样做但仍然收到错误消息
$position = Position::orderBy(DB::raw('(CASE WHEN position_value = 1 THEN created_at END asc, CASE WHEN position_value = 2 THEN created_at END desc)'))->paginate(29);
这是错误消息
SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'asc, CASE WHEN position_value = 2 THEN created_at END desc) asc limit 29 offset 0' at line 1 (SQL: select * from `position` order by (CASE WHEN position_value = 1 THEN created_at END asc, CASE WHEN position_value = 2 THEN created_at END desc) asc limit 29 offset 0)
感谢
答案 0 :(得分:2)
删除额外的括号并使用orderByRaw
方法
$position = Position::orderByRaw('CASE WHEN position_value = 1 THEN created_at END asc, CASE WHEN position_value = 2 THEN created_at END desc')
->paginate(29);