prattom
prattom

Reputation: 1743

combining date and time columns for query in codeigniter

I have a database table with separate date and time column. To get particular I execute query in mysql in following manner

select * from table_A where empID='A1201' order by TIMESTAMP(date,time) desc limit 1

How can I convert this particular for codeigniter? I tried in following manner but it's not working

$column = 'TIMESTAMP(date,time)';
$this->db->select('*');
$this->db->where('empID', 'A1201');
$this->db->from('table_A');
$this->db->order_by($column, 'desc');
$this->db->limit(1);
$query = $this->db->get();
$data = $query->result();
return $data;

This query result in error since it executes in following manner ORDER BY TIMESTAMP(date DESC, time) DESC while the correct way is ORDER BY TIMESTAMP(date,time) DESC. What will be the correct way for codeigniter using active record

Upvotes: 0

Views: 328

Answers (1)

Atural
Atural

Reputation: 5439

try this

$query = $this->db
    ->select('*')
    ->where('empID', 'A1201')
    ->from('table_A')
    ->order_by($column, 'desc', false)
    ->limit(1)
    ->get();

order by comes with a 3rd option - just set this to false

You can get more infos about that in their documentation here

Upvotes: 1

Related Questions