2013-04-24 89 views
0

目前我正在用CodeIgniter製作一個搜索頁面。我面臨的問題是我的products表中有兩列,brandmodel。我想在此基礎上有三種狀態如圖所示的查詢來選擇結果:
如何連接兩列並對連接列使用like子句?

查詢僅包含品牌
查詢僅包含模型
查詢包含

前兩個查詢做工精細,但問題是當查詢同時包含brandmodel時。我如何在brandmodel加入的列中使用LIKE子句?

+0

全文索引會爲您節省? – Nemoden 2013-04-24 01:47:08

+0

@Nemoden解釋一下。 – 2013-04-24 01:49:11

+0

[全文索引](http://bit.ly/13tyHP7) – Nemoden 2013-04-24 01:51:26

回答

0

如果你只是想找到同時包含一個特定品牌和特定模型中的行,你可以只使用兩個相同的語句在where子句:

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

0

對於靈活查詢,你最好使用的笨活動記錄類:

$this->db->like('model', 'model'); 
$this->db->like('brand', 'brand'); 
$this->db->select('*'); 
$this->db->get('products'); 
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();