如何使用 CodeIgniter 的 Active Record 方法添加 ORDER BY 子句? [英] How to add an ORDER BY clause using CodeIgniter's Active Record methods?

查看:38
本文介绍了如何使用 CodeIgniter 的 Active Record 方法添加 ORDER BY 子句?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个非常小的脚本来从数据库表中获取所有记录,代码如下.

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();

使用此语法,我如何将 ORDER BY 'name' 子句添加到我的选择查询中?

Using this syntax, how would I add a ORDER BY 'name' clause to my select query?

每次我把顺序一点一点地放在最后时都会出错.

I get errors every time I stick the order by bit on the end.

推荐答案

我相信 get() 函数会立即运行选择查询并且不接受 ORDER BY 条件作为参数.我认为您需要单独声明条件,然后运行查询.试试这个:

I believe the get() function immediately runs the select query and does not accept ORDER BY conditions as parameters. 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 文档 order_by()

这篇关于如何使用 CodeIgniter 的 Active Record 方法添加 ORDER BY 子句?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆