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

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

  • 写回答

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();
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 maya的mel里,怎样先选择模型A,然后利用mel脚本自动选择有相同名字的模型B呢。
  • ¥15 Python题,根本不会啊
  • ¥15 Python题,不会啦
  • ¥15 LD衰减结果文件dist的单位?
  • ¥15 Python题,回答一下下啦
  • ¥15 会会信号与系统和python的来
  • ¥15 关于#python#的问题
  • ¥20 oracle RAC 怎么配置啊,配置
  • ¥15 excel 日常使用中出现问题
  • ¥20 pdusession建立失败