[英]laravel 7: how to display data with if / case statements in the raw / query builder
我想向 select 查詢添加一個名為 status 的新字段,其中該字段的條件是使用 case / if 語句的 select 查詢的結果。
這是我現在的桌子:
全名 | 活躍 |
---|---|
X先生 | 錯誤的 |
Z先生 | 真的 |
查詢后我想要的期望:
全名 | 活躍 | 地位 |
---|---|---|
X先生 | 錯誤的 | 是的 |
Z先生 | 真的 | 不 |
這是我的代碼,但仍然顯示錯誤: Undefined column: 7 ERROR: column "0" does not exist LINE 2: WHEN app.is_active = "0" THEN "Yes"
$data = DB::table('my_table as app')
->select('fullname','is_active',
DB::raw('(CASE
WHEN app.is_active = "0" THEN "Yes"
ELSE "No"
END) AS status'))
->get();
非常感謝。
嘗試對 MySQL 字符串文字使用單引號:
$data = DB::table('my_table as app')
->select('fullname', 'is_active',
DB::raw("CASE app.is_active WHEN '0' THEN 'Yes' ELSE 'No' END AS status"))
->get();
如果以上解決了您的問題,那么我實際上會有點驚訝,因為 MySQL 通常會接受字符串文字的雙引號。 您看到的錯誤消息暗示"0"
被解釋為列名。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.