【问题标题】:Laravel-5.6 'LIKE' in where selectLaravel-5.6 'LIKE' 在哪里选择
【发布时间】:2019-07-19 21:37:47
【问题描述】:

我使用这个查询,我得到一个错误:

 $description = $request->get('description');
        if (!empty($description)){
        $description_query = Transcationhistorique::where(['sender_id' => $user_id, "%$description%", 'LIKE','description'])
            ->orWhere('receiver_id', $user_id)->get();
        }else{
            $description_query  =  "" ;
        }

这是我得到的错误:

"SQLSTATE[42S22]: 找不到列: 1054 未知列 '0' 在 'where 子句' (SQL: select * from transcation_historique where (sender_id = 32 和 0 = %salaire% 和 1 = LIKE 和 2 = 描述)或receiver_id = 32)”

这是我真正想要运行的:

select * from `transcation_historique` where (`sender_id` = 32 and `description` = %salaire%) or `receiver_id` = 32)

【问题讨论】:

    标签: php mysql laravel laravel-5 laravel-5.6


    【解决方案1】:

    试试这个,

    $description_query = Transcationhistorique::where(
                             function($query) use ($user_id, $description){
                                 return $query->where('sender_id', $user_id)
                                              ->where('description', 'like', '%' . $description .'%');
    
                             }
                         )
                         ->orWhere('receiver_id', $user_id)
                         ->get();
    

    【讨论】:

    • 我总是得到所有的交易
    • 您确定收到的描述不是空的吗?
    • 我认为在所有情况下,这种情况都在执行,您将获得所有交易。 ->orWhere('receiver_id', $user_id)
    【解决方案2】:

    您的 where 查询似乎结构不正确。 如果要使用“=”以外的运算符,则应使用以下结构 Source

    $query->where([
        ['column_1', '=', 'value_1'],
        ['column_2', '<>', 'value_2'],
        [COLUMN, OPERATOR, VALUE],
        ...
    ])
    
    

    我的建议是:

     $description = $request->get('description');
     if (!empty($description)){
         $description_query = Transcationhistorique::where([
                ['sender_id', '=', $user_id],
                ['description', 'LIKE', "%{$description}%"]
         ])
         ->orWhere('receiver_id', $user_id)->get();
     }else{
         $description_query  =  "" ;
     }
    
    

    【讨论】:

    • 我总是得到所有的交易
    【解决方案3】:

    试试这个:

    Transcationhistorique::where('receiver_id', '=', $user_id)
            ->orWhere(function ($query) use ($user_id, $description) {
                $query->where('sender_id', '=', $user_id)->where('description', 'LIKE', "%".$description."%");
            })->get();
    

    【讨论】:

      【解决方案4】:

      您可以使用多个where 方法作为and。 您可以尝试以下代码吗?

          $description = $request->get('description');
          if (!empty($description)){
              $description_query = Transcationhistorique::where('sender_id', $user_id)
              ->where("description", 'LIKE','%' . $description . '%')
              ->orWhere('receiver_id', $user_id)->get();
          }else{
              $description_query  =  "" ;
          }
      

      【讨论】:

      • 看看他的手动查询。 where( 多个条件) 或 where( 单个条件)
      • 我总是得到所有的交易
      • 我听不懂你?我说where“方法可以使用很多次”。
      • 只是我没有得到空的描述行,但我得到了所有行
      • 如果您想在查询中使用两个and 一个or 条件。我的解决方案会奏效。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-09-08
      • 1970-01-01
      • 2019-01-31
      • 2013-08-05
      • 1970-01-01
      • 2015-10-24
      • 1970-01-01
      相关资源
      最近更新 更多