由于poscms是基于CI框架的,所以CI中常见的数据库查询语句是该熟悉一点,所以在这里做个记录。
//一般查询
$this->db->select('name,grade')->where('sex','男')->limit(10,10)->get('tableName')->result();
一、查询结果集
->result();---------------------------------------------------返回object数组(多条记录)
->result_array();-------------------------------------------返回二维数组
->row();------------------------------------------------------返回一个对象(一条记录)
->row_array();----------------------------------------------返回一维数组
$this->db->insert_id();------------------------------------上一条插入的数据记录的id值(数据插到主表后,立即插到附表的话,id关联的时候回用到)
二、条件查询
->where('name','Jack');-----------------------------------条件是姓名为Jack
$ids = [1,2,3,4,5]
->where_in('id',[Math Processing Error]ids);−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−ids是一个数组
->where_not_in('id',$ids);--------------------------------与上对立
$array = array('name !=' => $name, 'id <' => $id, 'date >' => $date);
->where($array);-------------------------------------------根据多个条件来筛选
$where = "name='Joe' AND status='boss' OR status='active'";
->where($where);------------------------------------------根据自定义的sql模式的where语句进行筛选
->like('title', 'match', 'before');---------------------------模糊匹配,第三个参数可选,不选的话就是下面第三种情况,%match
->like('title', 'match', 'after');-----------------------------模糊匹配,match%
->like('title', 'match', 'both');-----------------------------模糊匹配,%match%
->not_like('title', 'match', 'both')------------------------模糊匹配,与上对立
$array = array('title' => $match, 'page1' => $match, 'page2' => $match);
->like([Math Processing Error]array)−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−模糊匹配关联数组,match%
三、distinct去重
->select('username,grade')->distinct();---------------根据用户名和成绩去重,只有用户名和成绩都相同才会被去重
四、排序
->order_by('title DESC, name ASC');-----------------title降序name升序
->order_by('name','RANDOM');------------------------随机排序,第一个参数无实际意义,但是要写,随机嘛,谈不上根据哪个字段随机了
五、分页
->limit(10);---------------------------------------------------未设置偏移量,那么默认偏移量为0,即从0开始选择,选出10条数据
->limit(10,10);-----------------------------------------------设置了偏移量,从10开始,选出10条数据,即11-20条记录
六、计数
->count_all('tableName')---------------------------------查询数据表中总的记录数
->where($where)->from('tableName')->count_all_results();查询符合当前条件的记录的数目,注意用from而不是get
->where($where)->count_all_results('tableName');效果同上
七、插入记录
$data = array(
'title' => 'My title',
'name'=>'My name',
);
->insert('tableName',$data);-----------------------------往表中插入一条数据
$data = array(
array(
'title' => 'My title',
'name'=>'My name',
),
array(
'title' => 'My title',
'name'=>'My name',
)
);
->insert_batch('tableName',$data);--------------------往表中插入多条记录
八、更新
$data = array(
'title' => 'My title',
'name'=>'My name',
);
->where('id',5)->updata('tableName',$data);--------更新主键(id)为5的记录的相关数据
九、删除
$this->db->where('id', $id);
$this->db->delete('mytable');
十、统计某一列的值
$tj_list = $this->game_user_log->instance_total()->select_sum('total');
class demo_model extends CI_Model {
public function __construct()
{
$this->load->database();
}
//查询实例
public function select()
{
//SELECT * FROM mytable
$query = $this->db->get('mytable');
//SELECT * FROM mytable LIMIT 20, 10
//说明:第二参数是每页纪录数,第三个参数是偏移
$query = $this->db->get('mytable', 10, 20);
//SELECT * FROM mytable where id=$id LIMIT $offset,$limit
$query = $this->db->get_where('mytable',array('id' => $id),$limit,$offset);
//SELECT title, content, date FROM mytable
$this->db->select('title, content, date');
$query = $this->db->get('mytable');
//SELECT MAX(age) as age FROM members
$this->db->select_max('age');
$query = $this->db->get('members');
//SELECT MIN(age) as age FROM members
$this->db->select_min('age');
$query = $this->db->get('members');
//SELECT MAX(age) as member_age FROM members
$this->db->select_max('age', 'member_age');
$query = $this->db->get('members');
//SELECT SUM(age) as member_age FROM members
$this->db->select_sum('age','member_age');
$query = $this->db->get('members');
//SELECT count(*) as total FROM members
$this->db->select('count(*) as total');
$query = $this->db->get('members');
//SELECT title, content, date FROM mytable
$this->db->select('title, content, date');
$this->db->from('mytable');
$query = $this->db->get();
//SELECT * FROM blogs
//JOIN comments ON comments.id = blogs.id
$this->db->select('*');
$this->db->from('blogs');
$this->db->join('comments', 'comments.id = blogs.id');
$query = $this->db->get();
//可选参数:left,right, outer, inner, left outer,right outer.
//LEFT JOIN comments ON comments.id = blogs.id
$this->db->join('comments', 'comments.id = blogs.id', 'left');
//WHERE name = 'joe'
$this->db->where('name','joe');
//WHERE name = 'joe' and id=1
$this->db->where('name','joe');
$this->db->where('id',1);
//WHERE name != 'joe' and id < 45
$this->db->where('name !=', 'joe');
$this->db->where('id <', 45);
//WHERE name = 'Joe' AND title = 'boss'
$array = array('name' => $name, 'title' => $title);
$this->db->where($array);
//WHERE name != 'Joe' AND id<1
$array = array('name!=' => $name, 'id<' => $id);
$this->db->where($array);
//可以自定义where
$where = "name='Joe' AND status='boss' OR status='active'";
$this->db->where($where);
//WHERE name != 'Joe' OR id > 50
$this->db->where('name !=', $name);
$this->db->or_where('id >', $id);
//WHERE username IN ('Frank', 'Todd', 'James')
$names = array('Frank', 'Todd', 'James');
$this->db->where_in('username', $names);
//OR username IN ('Frank', 'Todd', 'James')
$names = array('Frank', 'Todd', 'James');
$this->db->or_where_in('username', $names);
//WHERE username NOT IN ('Frank', 'Todd', 'James')
$names = array('Frank', 'Todd', 'James');
$this->db->where_not_in('username', $names);
//OR username NOT IN ('Frank', 'Todd', 'James')
$names = array('Frank', 'Todd', 'James');
$this->db->or_where_not_in('username', $names);
//WHERE title LIKE '%match%' AND body LIKE '%match%'
$this->db->like('title', 'match');
$this->db->like('body', 'match');
//可选参数:before,after,both,none
//WHERE title LIKE '%match'
$this->db->like('title', 'match', 'before');
// WHERE title LIKE '%match%' AND page1 LIKE '%match%' AND page2 LIKE '%match%'
$array = array('title' => $match, 'page1' => $match, 'page2' => $match);
$this->db->like($array);
// WHERE title LIKE '%match%' OR body LIKE '%match%'
$this->db->like('title', 'match');
$this->db->or_like('body', $match);
// WHERE title NOT LIKE '%match%
$this->db->not_like('title', 'match');
// WHERE title LIKE '%match%' OR body NOT LIKE '%match%'
$this->db->like('title', 'match');
$this->db->or_not_like('body', 'match');
//GROUP BY title
$this->db->group_by("title");
//GROUP BY title, date
$this->db->group_by(array("title", "date"));
//ORDER BY title DESC
$this->db->order_by("title", "desc");
//ORDER BY title DESC, name ASC
$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");
//LIMIT 10
$this->db->limit(10);
//LIMIT 20, 10 (仅限MySQL中。其它数据库有稍微不同的语法)
$this->db->limit(10, 20);
//$this->db->count_all_results('my_table');
}
//插入实例
public function insert()
{
//INSERT INTO mytable (title, name, date) VALUES ('My title', 'My name', 'My date')
$data = array('title' => 'My title' ,'name' => 'My Name' ,'date' => 'My date');
$this->db->insert('mytable', $data);
//INSERT INTO mytable (title, name, date) VALUES ('My title', 'My name', 'My date'),
//('Another title', 'Another name', 'Another date')
$data = array(
array(
'title' => 'My title' ,
'name' => 'My Name' ,
'date' => 'My date'
),
array(
'title' => 'Another title' ,
'name' => 'Another Name' ,
'date' => 'Another date'
)
);
$this->db->insert_batch('mytable', $data);
//INSERT INTO mytable (field) VALUES (field+1)
$this->db->set('field', 'field+1', FALSE);
$this->db->insert('mytable');
//INSERT INTO mytable (field) VALUES ('field+1')
//$this->db->set('wealth', 'wealth-'.$wealth, FALSE);
$this->db->set('field', 'field+1');
$this->db->insert('mytable');
//
$array = array('name' => $name, 'title' => $title, 'status' => $status);
$this->db->set($array);
$this->db->insert('mytable');
}
//更新实例
public function update()
{
// UPDATE mytable
// SET title = '{$title}', name = '{$name}', date = '{$date}'
// WHERE id = $id
$data = array( 'title' => $title,'name' => $name,'date' => $date);
$this->db->where('id', $id);
$this->db->update('mytable',$data);
//
$this->db->update('mytable', $data, "id = 4");
//
$where = array('id' => $id);
$this->db->update('mytable', $data, $where);
}
//删除实例
public function delete()
{
//DELETE FROM mytable WHERE id = $id
$this->db->delete('mytable', array('id' => $id));
//DELETE FROM table1 WHERE id = 5
//DELETE FROM table2 WHERE id = 5
//DELETE FROM table3 WHERE id = 5
$tables = array('table1', 'table2', 'table3');
$this->db->where('id', '5');
$this->db->delete($tables);
// DELETE FROM mytable
$this->db->empty_table('mytable');
}
//其他实例
public function other()
{
//链式方法
$this->db->select('title')->from('mytable')->where('id', $id)->limit(10, 20);
$query = $this->db->get();
//Active Record 缓存
$this->db->start_cache();
$this->db->select('field1');
$this->db->stop_cache();
$this->db->get('tablename');
//此处输出:SELECT `field1` FROM (`tablename`)
$this->db->select('field2');
$this->db->get('tablename');
//此处输出:SELECT `field1`, `field2` FROM (`tablename`)
$this->db->flush_cache();
$this->db->select('field2');
$this->db->get('tablename');
//此处输出:SELECT `field2` FROM (`tablename`)
}
//显示结果
public function result()
{
//原生写法
$sql="select * from user";
$result=$this->db->query($sql)->result_array();
return $query->row_array();//返回第一行数据,返回结果为数组
return $query->result_array();//返回多行数据,返回结果为数组
return $query->row();//返回第一行数据
return $query->result();//返回多行数据
return $query->row_array(4);//加参数,可返回第五行数据,用法同上
return $query->num_rows();//当前请求行数
return $query->num_fields();//当前请求的字段数(列数)
$this->db->affected_rows();//更新插入显示影响结果
//显示结果集
return $this->db->query('sql');
//显示插入id
return $this->db->insert_id();
//显示my_table有多少行数据
return $this->db->count_all('my_table');
//显示系统使用的数据库
return $this->db->platform();
//输出系统正在运行的数据库版本号
return $this->db->version();
//显示最近执行的SQL语句:SELECT * FROM sometable....
return $this->db->last_query();
//生成一个SQL语句
//INSERT INTO table_name (name, email, url) VALUES ('Rick', '[email protected]', 'example.com')
$data = array('name' => $name, 'email' => $email, 'url' => $url);
return $this->db->insert_string('table_name', $data);
//同上
$data = array('name' => $name, 'email' => $email, 'url' => $url);
$where = "author_id = 1 AND status = 'active'";
return $this->db->update_string('table_name', $data, $where);
}
}
(我不怕千万人阻挡,只怕自己投降!)