Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert RIGHT LEFT functions to codeigniter active record

I have a query like this:

SELECT RIGHT(id, 1) id_root
FROM  user 
WHERE LENGTH(id) = 3
and LEFT(id, 1) = '0'

And how to convert that's query to active record in codeigniter.

My problem is with syntax RIGHT( id, 1 ) and also at LEFT(id,1)='0'

like image 796
nanasjr Avatar asked Jun 22 '15 06:06

nanasjr


Video Answer


1 Answers

    $result_arr = $this->db
        ->select("RIGHT(id, 1) id_root", FALSE)
        ->from("user")
        ->where(
                array("LENGTH(id)"=> 3, "LEFT(id, 1) =" => 0)
        )->get()
        ->result_array();

Or you can simply use $this->db->query("You SQL");

$query = "SELECT RIGHT(id, 1) id_root
          FROM  user 
          WHERE LENGTH(id) = ?
          and LEFT(id, 1) = ? ";
$result_arr = $this->db->query($query, array(3, 0))->result_array();
like image 127
TigerTiger Avatar answered Oct 11 '22 14:10

TigerTiger