原始查询CASE WHEN在Lumen中不起作用?

时间:2020-07-15 10:26:47

标签: sql laravel lumen case-when

我想在Lumen中使用CASE WHEN,但是它返回消息“糟糕!发生错误。服务器返回了500 Internal Server Error。”

//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

谢谢。

1 个答案:

答案 0 :(得分:1)

请尝试以下操作:

//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();