N B
N B

Reputation: 97

ORDER BY but make zeroes/nulls last by Adding minus in Order By in Codeigniter

I am trying to order by position on my query but set the zero to last. Here is the pseudo code

$this->db->select("id,position");
$this->db->order_by('-position', 'ASC' );
$result = $this->db->get( 'SampleTable' );

Codeigniter treats the query as

SELECT `id`,`position` FROM `TABLE` ORDER BY `-position` ASC thus having an error

Is there any way I can pass the - minus so that the query would be

SELECT `id`,`position` FROM `TABLE` ORDER BY -`position` ASC

Upvotes: 1

Views: 1484

Answers (1)

Saurabh Parekh
Saurabh Parekh

Reputation: 449

You can try this.

        $this->db->select("id,position");
        $this->db->order_by('(position * -1)', 'ASC' );
        $result = $this->db->get( 'SampleTable' );

Upvotes: 1

Related Questions