I need to execute this query with Codeigniter query builder:
SET @row_number:=0;
SELECT Id, @row_number:=@row_number+1 as Position
FROM my_table
WHERE date='2015-12-26'
I can do it by using "$this-> db-> query" function like this:
$query = 'SET @row_number:=0;';
$this-> db-> query ($query);
$query = 'SELECT Id, @row_number:=@row_number+1 as Position';
$query = $query . ' FROM my_table';
$query = $query . ' WHERE date=\'' . $data . '\''
But, my question is: Is there a way to do it without hard write the query, so writing something like this:
$query = 'SET @row_number:=0;';
$this-> db-> query ($query);
#*****It's wrong!!!!*****
$this -> db -> select(array('Id', '@row_number:=@row_number+1 as Position'));
$this -> db -> from('my_table');
$this -> db -> where('date', $data);
$query = $this -> db -> get();
No need to pass array in select
you just write your query as
$this -> db -> select('Id, @row_number:=@row_number+1 as Position');
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With