CodeIgniter Forums
two order by clause in sql query - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: two order by clause in sql query (/showthread.php?tid=27979)



two order by clause in sql query - El Forum - 02-26-2010

[eluser]core-coder[/eluser]
Hi, I want to execute a query with two order by clause as

Code:
SELECT * FROM `categories` ORDER BY `categories_order` ASC, `categories_name` ASC

How can I achieve using codeignter database class ?

Thank you guys


two order by clause in sql query - El Forum - 02-26-2010

[eluser]puzzlebox[/eluser]
is $this->db->query($query) not ok? hmmmm


two order by clause in sql query - El Forum - 02-26-2010

[eluser]Armchair Samurai[/eluser]
Assuming you mean CI Active Record, just call order_by() two times, once for each clause, just pass your own string to order_by().

Code:
$this->db->order_by('categories_order', 'asc');
$this->db->order_by('categories_name', 'asc');

$query = $this->db->get('categories');

// OR

$this->db->order_by('categories_order ASC, categories_name ASC');

$query = $this->db->get('categories');



two order by clause in sql query - El Forum - 02-26-2010

[eluser]core-coder[/eluser]
thanks but I am trying to get it by something like $this->db->order_by("categories_order", "ASC"); ...

ant hope ?


two order by clause in sql query - El Forum - 02-26-2010

[eluser]core-coder[/eluser]
[quote author="Armchair Samurai" date="1267189924"]Assuming you mean CI Active Record, just call order_by() two times, once for each clause, just pass your own string to order_by().

Code:
$this->db->order_by('categories_order', 'asc');
$this->db->order_by('categories_name', 'asc');

$query = $this->db->get('categories');

// OR

$this->db->order_by('categories_order ASC, categories_name ASC');

$query = $this->db->get('categories');
[/quote]

Thanks I got it by $this->db->order_by('categories_order ASC, categories_name ASC');