Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Codeigniter SET variable on query

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();
like image 656
Fabio Avatar asked Dec 26 '15 13:12

Fabio


1 Answers

No need to pass array in select you just write your query as

 $this -> db -> select('Id, @row_number:=@row_number+1 as Position');
like image 128
Saty Avatar answered Nov 06 '22 22:11

Saty