0

目前我正在使用 CodeIgniter 制作搜索页面。我面临的问题是我的products表中有两列,brand并且model. 我正在尝试根据具有三种状态的查询来选择结果,如图所示:

查询只包含品牌
查询只包含型号
查询包含两者

前两个查询工作正常,但问题是当查询同时包含brand和时model。如何对两个列brandmodel连接列使用 LIKE 子句?

4

3 回答 3

0

对于灵活的查询,最好使用 CodeIgniter 活动记录类:

$this->db->like('model', 'model');
$this->db->like('brand', 'brand');
$this->db->select('*');
$this->db->get('products');
于 2013-04-24T02:55:05.537 回答
0

如果您只是想查找同时包含特定品牌和特定型号的行,则可以在 where 子句中使用两个 like 语句:

select * from products where model like '%MODEL%' and brand like '%BRAND%'

于 2013-04-24T02:39:38.410 回答
0

用这个:

if(!empty($brandsearchtext)){
    $this->db->like('p.brand', $this->db->escape_like_str($brandsearchtext));
}
if(!empty($modelsearchtext)){
    $this->db->like('p.model', $this->db->escape_like_str($modelsearchtext));
}

$this->db->select("*");
$this->db->from('products');

$result = $this->db->get();
//echo $this->db->last_query();
于 2013-04-24T10:06:35.543 回答