$this->db->query()方法执行。CI框架数据库查询语句详解

在CodeIgniter(简称CI)框架中,数据库操作是通过Active Record类来完成的,这个类提供了一种简单而强大的方法来执行SQL查询和操作,以下是一些常用的数据库查询语句及其使用方法:
一、基本查询
1.选择数据
使用select()方法指定要查询的列,然后使用get()方法获取结果。
$this->db->select('name, grade')->where('sex', '男')->limit(10, 10)->get('tableName')->result();select(): 指定要查询的字段。
where(): 添加条件。
limit(): 限制结果集的数量。
get(): 执行查询并返回结果。
2.查询结果集
result(): 返回一个对象数组(多条记录)。
result_array(): 返回二维数组。
row(): 返回一个对象(单条记录)。
row_array(): 返回一维数组。
3.插入数据
$data = array(
'title' => 'My title',
'name' => 'My name',
);
$this->db->insert('tableName', $data);insert(): 往表中插入一条数据。
insert_batch(): 往表中插入多条数据。

4.更新数据
$data = array(
'title' => 'My title',
'name' => 'My name',
);
$this->db->where('id', 5)->update('tableName', $data);where(): 指定更新条件。
update(): 更新数据。
5.删除数据
$this->db->where('id', $id);
$this->db->delete('mytable');where(): 指定删除条件。
delete(): 删除数据。
二、条件查询
1.简单条件
$this->db->where('name', 'Jack')->get('tableName')->result();where(): 添加条件。
2.复杂条件
$array = array('name !=' => $name, 'id <' => $id, 'date >' => $date);
$this->db->where($array)->get('tableName')->result();where(): 根据关联数组添加多个条件。
3.自定义SQL模式
$where = "name='Joe' AND status='boss' OR status='active'";
$this->db->where($where)->get('tableName')->result();where(): 根据自定义SQL模式筛选。
4.LIKE匹配
$this->db->like('title', 'match', 'before')->get('tableName')->result();like(): 模糊匹配。
三、去重查询
$this->db->select('username, grade')->distinct()->get('tableName')->result();distinct(): 去除重复记录。

四、排序查询
$this->db->order_by('title DESC, name ASC')->get('tableName')->result();order_by(): 对结果进行排序。
五、分页查询
$this->db->limit(10, 10)->get('tableName')->result();limit(): 限制结果集的数量。
六、计数查询
$this->db->count_all_results('tableName');count_all_results(): 查询数据表中总的记录数。
七、统计某一列的值
$tj_list = $this->game_user_log->instance_total()->select_sum('total');select_sum(): 统计某一列的总和。
八、示例代码
以下是一个综合示例,展示了如何使用上述方法进行数据库操作:
class Demo_model extends CI_Model {
public function __construct() {
parent::__construct();
$this->load->database(); // 加载数据库类
}
public function get_users($gender = NULL) {
if ($gender === NULL) {
return $this->db->get('users')->result_array(); // 获取所有用户
} else {
$this->db->where('gender', $gender); // 根据性别筛选用户
return $this->db->get('users')->result_array(); // 返回符合条件的用户数组
}
}
}在这个示例中,我们定义了一个模型类Demo_model,并在构造函数中加载了数据库类。get_users方法根据传入的性别参数获取相应的用户数据,如果未传入参数,则返回所有用户数据。
九、相关问题与解答
问题1: 如何在CI框架中执行原生SQL查询?
答: 在CI框架中,可以使用query()方法执行原生SQL查询。
$sql = "SELECT * FROM tableName WHERE name = 'John'"; $q = $this->db->query($sql);
通过这种方式,可以执行任何复杂的SQL语句。
问题2: 如何在CI框架中实现事务管理?
答: CI框架提供了简单的事务管理功能,以下是一个示例:
$this->db->trans_begin(); // 开始事务
$this->db->query("SQL query 1"); // 执行第一条SQL语句
$this->db->query("SQL query 2"); // 执行第二条SQL语句
if ($this->db->trans_status() === FALSE) { // 如果事务失败
$this->db->trans_rollback(); // 回滚事务
} else {
$this->db->trans_commit(); // 提交事务
}使用trans_begin()开始事务,trans_status()检查事务状态,trans_rollback()回滚事务,trans_commit()提交事务。
小伙伴们,上文介绍了“ci框架数据库查询语句”的内容,你了解清楚吗?希望对你有所帮助,任何问题可以给我留言,让我们下期再见吧。
文章来源网络,作者:运维,如若转载,请注明出处:https://shuyeidc.com/wp/44371.html<
