【问题标题】:How to add single quote instead of double quote with raw query laravel?如何使用原始查询 laravel 添加单引号而不是双引号?
【发布时间】:2021-01-19 22:54:22
【问题描述】:

以下是我的查询;

select "column1", "desc", if r_id = P then Y else 
'N' endif As is_selected from "other" where 
"rtype" = D_STATUS

下面是我的查询生成器函数,

DB::table('other')
->select('r_id', 'description', DB::raw("if r_id ='P' then 'Y' else 'N' endif As is_selected")) //Error
->where('rtype', '=',  $strType )
->get();

这里出现错误;

未找到“Y”列。

我想在查询中的所有静态字符串值中添加单引号而不是双引号。我该怎么做?

【问题讨论】:

  • 是的,我只需要在 Y 列和 N 列周围添加单引号。你能告诉我该怎么做吗? @KamleshPaul
  • 没有反引号键不起作用@KamleshPaul
  • 我认为您的代码是正确的,因为错误提示 Column "Y" not found 表示表中此列不存在
  • 不,我的查询工作正常。这个查询只是缺少围绕“Y”的单引号@KamleshPaul

标签: php laravel laravel-5 laravel-query-builder


【解决方案1】:

试试this way:

DB::table('other')
->select('r_id', 'description', DB::raw("if (r_id ='P','Y','N') as is_selected")) 
->where('rtype', '=',  $strType )
->get();

【讨论】:

    【解决方案2】:

    你可以使用反冲。

    DB::table('other')
    ->select('r_id', 'description', DB::raw("if r_id ='P' then \"Y\" else \"N\" endif As is_selected")) //Error
    ->where('rtype', '=',  $strType )
    ->get();
    

    here 是文档

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-05-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-12-08
      • 2022-01-04
      • 2012-10-14
      • 2017-07-09
      相关资源
      最近更新 更多