raw查询在lumen中不工作时的大小写?

dnph8jn4  于 2021-07-26  发布在  Java
关注(0)|答案(1)|浏览(382)

我想在lumen中使用case,但它返回消息“oops!发生错误。服务器返回500内部服务器错误。“

//Working
$result = DB::table('Users')
    ->select(DB::raw('count(*) as user_count'))
    ->where('user_id', 'A1')        
    ->get();
// Working
$result = DB::select('SELECT CASE WHEN user_status="new" THEN "New"  ELSE "Expired" END AS new_status FROM Users WHERE user_id="A1"');
//Failed
$result = DB::table('Users')
    ->select(DB::raw('CASE WHEN user_status="new" THEN "New"  ELSE "Expired" END AS new_status'))
    ->where('user_id', 'A1')        
    ->get();

为什么失败了?
流明7.0
谢谢您。

jjhzyzn0

jjhzyzn01#

请试试这个:

//Try
$result = DB::table('Users')
    ->select(DB::raw('CASE WHEN user_status=''new'' THEN ''New''  ELSE ''Expired'' END AS new_status'))
    ->where('user_id', 'A1')        
    ->get();

//Try2
$result = DB::table('Users')
    ->select(DB::raw("(CASE WHEN (user_status='new') THEN 'New'  ELSE 'Expired' END) AS new_status"))
    ->where('user_id', 'A1')        
    ->get();

相关问题