2013-11-02 24 views
0

我需要做的是這樣的SQL查詢,但在笨SQL查詢:與像和或組合

SELECT `table`.* FROM (`table`) WHERE `name` LIKE '%9%' OR `id` LIKE '%9%' AND (`name` LIKE '%f%' OR `id` LIKE '%f%') 

我試圖做絲毫此代碼:

$this->db->select('table.*'); 
$this->db->from('table'); 
foreach($data as $d){ 
    $this->db->like("name", $d) 
    $this->db->or_like("id", $d); 
} 

但結果在SQL是:

SELECT `table`.* FROM (`table`) WHERE `name` LIKE '%9%' OR `id` LIKE '%9%' AND `name` LIKE '%f%' OR `id` LIKE '%f%' 

感謝

回答

0

CI v2中沒有括號。你需要使用你的where()函數,如:

->where('mysql in here', null, true); 

null在這種情況下只是一個佔位符; TRUE不會添加`(反引號),而這些通常只是在這裏。您可以在用戶指南中看到更多內容

0
$this->db->select('*'); 
$this->db->from('tablename'); 
$this->db->like('name','9','both'); 
$this->db->or_like('id','9','both'); 
$this->db->like('name','f','both'); 
$this->db->or_like('id','f','both');