dqpwdai095465
2016-06-04 13:57
采纳率: 100%
浏览 36
已采纳

SQL - 选择它是否包含字符串

I'm trying to make an sql query which picks all the trucks with the searchTerm in the name

 $foodtrucks= DB::table('foodtrucks')
        ->rightjoin('users', 'foodtrucks.user_id', '=', 'users.id')
        ->orderBy('users.premium_type','premium')
        ->select('foodtrucks.*')
        ->where('name', 'LIKE', $searchTerm)
        ->orWhere('info', 'LIKE', $searchTerm)
        ->get();

I'm currently getting the results if I write te exact name but this is not what I want to happen, I want it to display the result even it the name contains the searchTerm

so for example

  • I search "Truck"

  • I get Truck123, Truck456 and Truck789

What's happening now

  • I search "Truck"

  • I get no results


  • I search Truck123

  • I get Truck123

图片转代码服务由CSDN问答提供 功能建议

我正在尝试使用名称< searchTerm选择所有卡车的SQL查询 / strong>

  $ foodtrucks = DB :: table('foodtrucks')
  - &gt; rightjoin('users','foodtrucks.user_id','=',  'users.id')
  - &gt; orderBy('users.premium_type','premium')
  - &gt; select('foodtrucks。*')
  - &gt; where('name','LIKE'  ,$ searchTerm)
  - &gt; orWhere('info','LIKE',$ searchTerm)
  - &gt; get(); 
   
 
 

我' 我目前得到的结果如果我写了确切的名字,但这不是我想要发生的,我希望它显示结果,即使名称包含searchTerm

所以 例如

  • 我搜索“卡车”

  • 我得到Truck123, Truck456和Truck789

    现在发生了什么

    • 我搜索“卡车”

    • 我没有结果


      \ n
      • 我知道 arch Truck123

      • 我得到Truck123

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • dongpu1881 2016-06-04 14:01
    已采纳

    So you are searching for LIKE %Truck%. Go for:

    $foodtrucks= DB::table('foodtrucks')
            ->rightjoin('users', 'foodtrucks.user_id', '=', 'users.id')
            ->orderBy('users.premium_type','premium')
            ->select('foodtrucks.*')
            ->where('name', 'LIKE', "%$searchTerm%")
            ->orWhere('info', 'LIKE', "%$searchTerm%")
            ->get();
    
    已采纳该答案
    打赏 评论

相关推荐 更多相似问题