简体   繁体   English

SQL Where子句与定界符

[英]SQL Where Clause with Delimiter

How do I write a SQL search statement where the input is a space separated string and each word belongs to a different column? 如何编写一条SQL搜索语句,其中输入是一个用空格分隔的字符串,并且每个单词都属于不同的列?

For example: 例如:

Search Input: 2011 Audi Q7 搜索输入: 2011年奥迪Q7

Where "2011" searches for the year, 在“ 2011”搜索年份的地方,

"Audi" searches for the make, “奥迪”搜索品牌,

and "Q7" searches for the model 和“ Q7”搜索模型

I'm using AJAX to create a filter for my SQL table, here's the code I'm working with, but isn't working: 我正在使用AJAX为我的SQL表创建过滤器,这是我正在使用的代码,但无法正常工作:

Database 数据库

|year|make|model|
----------------
|2017|Audi|Q7
----- ---- -----
 ... | ...| ...

PHP 的PHP

public function categoryOptions() {
    $term = $this->request->input('term');
    $options = VehiclesModel::where('year', 'like', '%'.$term.'%')->where('make', 'like', '%'.$term.'%')->where('model', 'like', '%'.$term.'%')->get();
    return $options->pluck('id','year','make','model');
}

I think I need to use a delimiter in '%'.$term.'%' but not sure how thats done. 我想我需要在'%'.$term.'%'使用定界符,但不确定如何完成。

Split $term by ' ' and you will have an array with year in [0], make in [1] and model in [2]. 将$ term用' '分割,您将得到一个数组,其中year在[0]中,make在[1]中,模型在[2]中。 Something like: 就像是:

public function categoryOptions() {
    $term = $this->request->input('term');
    $terms = explode(' ', $term);
    $options = VehiclesModel::where('year', 'like', '%'.$terms[0].'%')->where('make', 'like', '%'.$terms[1].'%')->where('model', 'like', '%'.$terms[2].'%')->get();
    return $options->pluck('id','year','make','model');
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM