2010-04-08 22 views
0

我有一個像這樣在我的數據庫表命名訪問:如何選擇每個月的數據庫內容?

ID IP ACTION_DATE | TIME_STAMP

我用這個代碼來店實地考察

/* Hits table has an auto-incrementing id and an ip field */ 

     // Grab client IP 
     $ip = $this->input->ip_address(); 

     // Check for previous visits 
     $query = $this->db->get_where('visits', array('ip' => $ip), 1, 0); 
     $query = $query->row_array(); 

     if (count($query) < 1) 
     { 
      // Never visited - add 
      $this->db->insert('visits', array('ip' => $ip)); 
     } 

它的工作不錯。但我的客戶需要知道他們有多少次訪問。我怎樣才能做到這一點 ?坦克。

回答

1

如何像:

$months = $this->db->select(' 
    COUNT(`id`) AS "count", 
    DATE_FORMAT(FROM_UNIXTIME(`action_date`),"%b-%Y") AS "month"',false 
) 
->group_by('month') 
->where(array('ip' => $ip)) 
->order_by('action_date','desc') 
->get('visits')->result(); 

應該給你喜歡的數據:

+-------+----------+ 
| count | month | 
+-------+----------+ 
| 505 | Apr-2010 | 
| 252 | Mar-2010 | 
| 426 | Feb-2010 | 
| 201 | Jan-2010 | 
| 211 | Dec-2009 | 
+-------+----------+ 
+0

我需要的地方爲一個月語句,因爲我必須證明我有多少人訪問對這個月沒怎麼很多人訪問這個$ ip地址。如果我能做到一切都會好起來的話,我不知道在action_date字段中的哪個位置指出月份。 – mehdi 2010-04-09 12:37:24