How to use special order_by of mysql in codeigniter

1

In mysql I can do this.

SELECT * FROM 'Cidade' ORDER BY 'id'=1347 DESC

But I can not do codeigniter, I'm trying like this:

$this->db->order_by("id = 1347", "DESC")
                ->get("Cidades");

In this case his output looks like this:

SELECT * FROM 'Cidade' ORDER BY 'id=1347' DESC

It is putting a block in the entire expression. it does not work there. Thanks in advance for your help.

    
asked by anonymous 29.03.2016 / 15:45

2 answers

1

The solution I found was writing to sql, not the best form but it worked. if someone has a better shape.

return $this->db->query("SELECT * FROM 'Cidade' 
                                ORDER BY id = 1347 DESC
                                , id = 1156 DESC
                                , id = 1284 DESC
                                , id = 1337 DESC
                                ");

Thanks!

    
29.03.2016 / 16:54
0

In the last parameter of the order_by method, put FALSE, so that the typed text is included with no change in what was written, ie the data will not be escaped, eg:

$this->db->order_by("id = 1347", "DESC", FALSE)
    
10.09.2018 / 18:54