[英]SQL Where Clause with Delimiter
如何编写一条SQL搜索语句,其中输入是一个用空格分隔的字符串,并且每个单词都属于不同的列?
例如:
搜索输入: 2011年奥迪Q7
在“ 2011”搜索年份的地方,
“奥迪”搜索品牌,
和“ Q7”搜索模型
我正在使用AJAX为我的SQL表创建过滤器,这是我正在使用的代码,但无法正常工作:
数据库
|year|make|model|
----------------
|2017|Audi|Q7
----- ---- -----
... | ...| ...
的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');
}
我想我需要在'%'.$term.'%'
使用定界符,但不确定如何完成。
将$ term用' '
分割,您将得到一个数组,其中year在[0]中,make在[1]中,模型在[2]中。 就像是:
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.