首页 > 学院 > 开发设计 > 正文

CI(5) SQL常用操作

2019-11-06 08:19:54
字体:
来源:转载
供稿:网友
注:执行SQL语句时,首先要确保已经与数据库链接:$this->db->database();1、Select查询    (1)获取数据表所有记录(取回全部数据,相当于 select * ):
1)$query = $this->db->get('sites'); //sites为表名
2)$this->db->from('sites');
   $query = $this->db->get();(2)获取数据表中特定的字段:select
$this->db->select('url','name','clientid'); //'url','name','clientid'为数据表字段$query = $this->db->get('sites');(3)排序:order_by

$this->db->order_by();

帮助你设置一个 ORDER BY 子句。第一个参数是你想要排序的字段名。第二个参数设置结果的顺序,可用的选项包括asc(升序)或desc(降序), 或random(随机)。

$this->db->order_by("title", "desc");// 生成: ORDER BY title DESC

你也可以在第一个参数中传递你自己的字符串:

$this->db->order_by('title desc, name asc');// 生成: ORDER BY title DESC, name ASC

或者,多次调用本函数就可以排序多个字段。

$this->db->order_by("title", "desc");$this->db->order_by("name", "asc");    // 生成: ORDER BY title DESC, name ASC 

例:
$this->db->select('url','name','clientid');$this->db->order_by("name", "desc");$query = $this->db->get('sites');(4)限制返回数据记录条数:limit
$this->db->select('url','name','clientid');//'url','name','clientid'为列名$this->db->orderby("name", "desc");$this->db->limit(5); // 最初五条数据$query = $this->db->get('sites');注:$this->db->limit($pageSize,$start)$pageSize:每页显示条数$start:起始条数(5)条件:where
1)‘=’的情况:
$this->db->where('clientid', '1');  //clientid属性  "1"为属性值
2)‘!=’的情况:
$this->db->where('url !=', 'www.mysite.com');
$this->db->where('id >', '3');
3)多个where条件的情况:
$this->db->where('url !=','www.mysite.com');
$this->db->where('id >', '3');
4)WHERE…OR的情况:或
$this->db->where('url !=','www.mysite.com' );
$this->db->orwhere('url !=','www.anothersite.com' );5)like:
$this->db->like('title', 'match', 'before'); 
// 生成: WHERE title LIKE '%match' 
$this->db->like('title', 'match', 'after'); 
// 生成: WHERE title LIKE 'match%' 
$this->db->like('title', 'match', 'both'); 
// 生成: WHERE title LIKE '%match%'
(6)组合查询:join
$this->db->from('sites');$this->db->join('people', 'sites.peopleid = people.id');如:写个完整的查询
$this->db->select('url','name','clientid','people.surname AS client');
$this->db->where('clientid', '3');
$this->db->limit(5);
$this->db->from('sites');
$this->db->join('people', 'sites.clientid = people.id','LEFT');
$this->db->order_by("name", "desc");$this->db->group_by('clientid.id');
$query = $this->db->get();(7)显示查询结果:
在查询语句后加上下面这句话:
$query = $this->db->get();
如果有多个结果,他们被保存在$row对象中,可以用一个 foreach 循环:
foreach ($query->result() as $row)
{
   PRint $row->url;
   print $row->name;
   print $row->client;
}
如果我们只想要一个结果,它可以作为一个对象被返回, 或在这里当做一个$row数组
if ($query->num_rows() > 0) // $query->num_rows() 返回查询结果条数
{
   $row = $query->row_array();  // 返回结果为 数组
 
   print $row['url'];
   print $row['name'];
   print $row['client'];
}2、Insert 添加    方法一:先建个数组,把要insert的值放在数组里.如下:其中url/name/clientid/type均为数据表属性值
$data = array(
                'url' => 'www.mynewclient.com',
                'name' => 'BigCo Inc',
                'clientid' => '33',
                'type' => 'dynamic'
            );            $this->db->insert('sites', $data);
方法二:使用$this->db->set() 设置每一个值
$this->db->set('url', 'www.mynewclinet.com');
$this->db->set('name', 'BigCo Inc');
$this->db->set('clientid', '33');
$this->db->set('type', 'dynamic');
$this->db->insert('sites');3、Update 更新    注:先定位要更新的记录,再Update        方式一:使用数组形式
$this->db->where('id', '1');
$data = array(
'url' => 'www.mynewclient.com',
'name' => 'BigCo Inc',
'clientid' => '33',
'type' => 'dynamic'
);$this->db->update('sites', $data);方式二:使用$this->db->set() 设置每一个值
$this->db->where('id', '1');$this->db->set('url', 'www.mynewclinet.com');$this->db->set('name', 'BigCo Inc');$this->db->set('clientid', '33');$this->db->set('type', 'dynamic');$this->db->update('sites');4、Delete 删除    注:先定位要删除的记录,再 Delete
$this->db->where('id', '2');$this->db->delete('sites');5、检测数据库是否执行成功    (1)$this->db->affected_rows();    (2)$new_id_number = $this->db->insert_id(); // 执行insert添加时,可以通过此函数返回添加之后记录的ID

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表