2011-06-16 65 views
1

選擇行我想建立一個查詢像使用CodeIgniter的Active Record

SELECT username from users 
WHERE login_attempts > 3 AND last_attempt_time < 24 (hours). 

我有2個問題來構建上面的查詢。

  1. 我存儲在last_attempt_time日起DATETIME格式(2011-06-16 10點29分23秒) 我可以直接選擇一些列具有時間差不超過24小時從現在開始? (或者我必須選擇一行,然後檢查例如PHP的時差?)

  2. 我該如何使用CodeIgniter Active Record編寫此查詢? (我需要使用get_where嗎?)在他們的文檔中我找不到任何相關示例。

謝謝。

回答

9

試試這個:

$this->db->select('username'); 
$this->db->from('users'); 
$this->db->where('login_attempts >', 3); 
$this->db->where('last_attempt_time <', date('Y-m-d H:i:s', strtotime('-24 hours'))); 

$query = $this->db->get(); 

你可以在這裏找到完整的文檔:http://codeigniter.com/user_guide/database/active_record.html#select

2

一個小小的優化弗朗索瓦的回答是:

$this->db->select('username'); 
$this->db->where('login_attempts >', 3); 
$this->db->where('last_attempt_time <', date('Y-m-d H:i:s', strtotime('-24 hours'))); 

$query = $this->db->get('users'); 

對不起,把作爲一個答案,因爲意見不允許\ n。

相關問題