【发布时间】:2018-10-24 17:56:59
【问题描述】:
我正在尝试按列值对结果进行排序,但它不起作用
$users = Comment::select([
'id',
'comment',
'user_name',
'product_id',
'rating',
'country',
'status',
'pin',
'created_at',
])->where('shop_name',$shop)->where('product_id', $id)->with('images')->orderByRaw("IF(product_url = 'customer') DESC")->orderByRaw("product_url = manually ASC")->orderBy('pin', 'desc')->orderBy('rating', 'desc')->with('pages')->get();
我添加了这段代码
->orderByRaw("IF(product_url = 'customer') DESC")
我得到了这个错误
"SQLSTATE[42000]: 语法错误或访问冲突:1064 你有一个 SQL 语法错误;检查与您对应的手册 MySQL 服务器版本,用于在 ') DESC 附近使用正确的语法, product_url = 手动 ASC,
pindesc,ratingdesc' 在第 1 行(SQL: 选择id、comment、user_name、product_id、rating、country,status,pin,created_at来自commentsshop_name= 和product_id= 按 IF(product_url = 'customer') DESC, product_url 排序 = 手动 ASC,pindesc,ratingdesc)
【问题讨论】:
-
您可以将
IF结果包含在计算列中,然后对其进行排序。
标签: php mysql sql laravel laravel-5.5