2011-01-31 34 views
0

我試圖使用代碼點火器的活動記錄類,看起來像這樣建立一個查詢:代碼點火器查詢:如何建立這個查詢

SELECT * from contacts where account = 1 and (fname like 'k%' or lname like 'k%')

但我不知道如何使用功能做這個。

和想法?

回答

1

您還可以使用類似的查詢

$this->db->like('title', 'match'); 

// Produces: WHERE title LIKE '%match%' 

而且or_like

$this->db->like('title', 'match'); 
$this->db->or_like('body', $match); 
+0

+1對於笨的活動記錄提供了類似的功能。這比在select語句中編寫喜歡要容易得多。 – 2011-02-01 23:25:01

3
$this->db->where('account', '1'); 
$this->db->where(' (fname LIKE "k%" OR lname LIKE "k%") '); 
$query = $this->db->get('contacts'); 

然後就像正常的Codeigniter數據庫結果一樣處理$query

0

呦可以八方通運行它作爲一個正常的查詢:

$sql = "SELECT * from contacts where account = 1 and (fname like ? or lname like ?)"; 
$query = $this->db->query($sql, array($like1, $like2)); 

我用更多的時候這比活躍的記錄類,因爲我熱衷於編寫sql語句。

0

而不是使用正常的查詢,你可以使用來自ellislab論壇https://ellislab.com/forums/viewthread/185983/ 而不是使用

$this->db->like('location', $your_string); 

使用建議:

$this->db->where('location LIKE', '%'.$your_string.'%'); 

or_where,而不是or_like

0

您可以使用查詢分組從CI 3.0

$this->db->select('*')->from('contacts ') 
    ->group_start() 
      ->like('fname', 'k', 'after'); 
      ->or_like('lname', 'k', 'after'); 
    ->group_end() 
    ->where('account', 1) 
->get();