Convert sql query in query in Active Record in codeigniter

时间:2017-06-19 13:54:45

标签: php mysql sql codeigniter activerecord

I need to convert this query in an active Record query.

SELECT * FROM test WHERE CAST(date AS DATE) = '2017-06-19' AND `ip` = '::1'

this is what I tried but the where with the date does not work

 function search($date, $ip){
   $this->db->select('cast(date as Date)');
   $this->db->where('date', $date);
   $this->db->where('ip', $ip);
   return $this->db->get('visitas'); 
  }

this produce something like this

SELECT cast(fecha as Date) FROM (`visitas`) WHERE `ip` = '::1'

thank´s in advance.

1 个答案:

答案 0 :(得分:0)

我目前无法对此进行测试,但我相信这是您正在寻找的。

// Convert your date string to mysql datetime
$date = date( "Y-m-d" , strtotime("2017-06-19") );

// Build the query
$this->db->select('*')->from('visitas')
                      ->where('CAST(date AS DATE)=', $date)
                      ->where('ip', '::1');

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