[英]How to add an ORDER BY clause using CodeIgniter's Active Record methods?
I have a very small script to get all records from a database table, the code is below.我有一个非常小的脚本来从数据库表中获取所有记录,代码如下。
$query = $this->db->get($this->table_name);
return $query->result();
Using this syntax, how would I add a ORDER BY 'name'
clause to my select query?使用这种语法,我如何将ORDER BY 'name'
子句添加到我的选择查询中?
I get errors every time I stick the order by bit on the end.每次我最后一点一点地坚持顺序时,我都会出错。
I believe the get()
function immediately runs the select query and does not accept ORDER BY
conditions as parameters.我相信get()
函数会立即运行选择查询并且不接受ORDER BY
条件作为参数。 I think you'll need to separately declare the conditions, then run the query.我认为您需要单独声明条件,然后运行查询。 Give this a try:试试这个:
$this->db->from($this->table_name);
$this->db->order_by("name", "asc");
$query = $this->db->get();
return $query->result();
CodeIgniter Documentation order_by()
CodeIgniter 文档order_by()
Using this code to multiple order by in single query.使用此代码在单个查询中进行多个排序。
$this->db->from($this->table_name);
$this->db->order_by("column1 asc,column2 desc");
$query = $this->db->get();
return $query->result();
Simple and easy:简单易行:
$this->db->order_by("name", "asc");
$query = $this->db->get($this->table_name);
return $query->result();
Just add the'order_by' clause to your code and modify it to look just like the one below.只需将“order_by”子句添加到您的代码中并将其修改为如下所示。
$this->db->order_by('name', 'asc');
$result = $this->db->get($table);
There you go.你去吧。
Try This: 试试这个:
$this->db->select('main.*');
$this->db->from("ci_table main");
$this->db->order_by("main.id", "DESC");
return $this->db->get()->result();
100% Working!!!! 100%工作!!!!
$this->db->order_by('price', 'ASC');
$q=$this->db->get('add_new_car');
return $q->result_array();
$query = $this->db->from("table_name")->order_by("table_name.column_name desc")->get();
return $query->result();
你也可以这样安排。
return $this->db->order_by('name', 'DESC')->get($this -> table_name)->result();
Two table join and order by name - 100% worked . 两个表连接和按名称排序 - 100%工作 。
$this->db->from('user_login');
$this->db->join('role','user_login.Roleid=role.Roleid');
return $this->db->order_by('Uname ASC')->get('')->result();
Simple and easy way: 简单方便:
// ascending order
$q=$this->db->select('*')->from('tbl_name')->order_by('name','asc')->get();
return $q->result();
// descending order
$q =$this->db->select('*')->from('tbl_name')->order_by('name','desc')->get();
return $q->result();
function getProductionGroupItems($itemId){
$this->db->select("*");
$this->db->where("id",$itemId);
$this->db->or_where("parent_item_id",$itemId);
/*********** order by *********** */
$this->db->order_by("id", "asc");
$q=$this->db->get("recipe_products");
if($q->num_rows()>0){
foreach($q->result() as $row){
$data[]=$row;
}
return $data;
}
return false;
}
Use order_by
: 使用order_by
:
$this->db->order_by("coloumn_name", "desc");
$query = $this->db->get('table_name');
return $query->result();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.