neel
neel

Reputation: 23

How to order by use in codeigniter

My table data is like that

ID NAME order
1 English 0
2 Italian 1
3 Spanish 2
4 Hindi 1
5 Bengali 3
6 Tamil 2

my query

    $this->db->select('*');
    $this->db->from('languages');
    $this->db->order_by('order','asc');
    $query = $this->db->get();

I want data like this order-1,1,2,2,3,0

ID NAME order
2 Italian 1
4 Hindi 1
3 Spanish 2
6 Tamil 2
5 Bengali 3
1 English 0

Upvotes: 1

Views: 243

Answers (1)

Ergest Basha
Ergest Basha

Reputation: 8973

The order = 0 is a boolean that evaluates to 1 when the value is 1 and otherwise 0. The order part orders the rest of the values in ascending order.

SELECT o.* 
FROM order_1 
ORDER BY `order`= 0, `order`;

See example here

Upvotes: 4

Related Questions