Laravel orderBy 列值错误
Laravel orderBy with column value error
我正在尝试按列值对结果进行排序,但它不起作用
$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]: 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 ') DESC,
product_url = manually ASC, pin
desc, rating
desc' at line 1 (SQL:
select id
, comment
, user_name
, product_id
, rating
,
country
, status
, pin
, created_at
from comments
where
shop_name
= and product_id
=
order by IF(product_url = 'customer') DESC, product_url
= manually ASC, pin
desc, rating
desc)
MySQL IF
函数接受 三个 个参数。
这个表达式无效:
IF(product_url = 'customer')
因为只有一个参数提供给 IF()
函数。
我们可以这样做:
IF(product_url = 'customer',1,0)
相当于更符合 ANSI 标准
CASE WHEN product_url = 'customer' THEN 1 ELSE 0 END
MySQL shorthand 也可以
ORDER BY product_url = 'customer' DESC
相当于
ORDER BY CASE
WHEN product_url = 'customer' THEN 1
WHEN product_url IS NOT NULL THEN 0
ELSE NULL
END DESC
我正在尝试按列值对结果进行排序,但它不起作用
$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]: 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 ') DESC, product_url = manually ASC,
pin
desc,rating
desc' at line 1 (SQL: selectid
,comment
,user_name
,product_id
,rating
,country
,status
,pin
,created_at
fromcomments
whereshop_name
= andproduct_id
= order by IF(product_url = 'customer') DESC, product_url = manually ASC,pin
desc,rating
desc)
MySQL IF
函数接受 三个 个参数。
这个表达式无效:
IF(product_url = 'customer')
因为只有一个参数提供给 IF()
函数。
我们可以这样做:
IF(product_url = 'customer',1,0)
相当于更符合 ANSI 标准
CASE WHEN product_url = 'customer' THEN 1 ELSE 0 END
MySQL shorthand 也可以
ORDER BY product_url = 'customer' DESC
相当于
ORDER BY CASE
WHEN product_url = 'customer' THEN 1
WHEN product_url IS NOT NULL THEN 0
ELSE NULL
END DESC