0

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

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.