IT俱乐部-城北日常经验分享

浏览: 3821    评论: 0

CodeIgniter (CI)框架中的数据库查询汇总


CodeIgniter (CI)框架中的数据库查询汇总

引言:

前两天业务涉及到一个拉取答题排行榜的需求,数据库里数据是这样的:

同一个人可能提交过多次成绩,所以同一个人可能会有多次记录;

同一个人提交的多次成绩中可能有至少两次成绩是一样的。

于是,查询的时候,首先查询出每个人的最高成绩记录,然后如果某个人的最高成绩记录有多条,去重!

最终sql语句如下:


/*拉取排行榜*/
    public function rank(){
        $data= json_decode(file_get_contents('php://input'), true);
        $name = $data['name'];
        $grade = $this->db->where('username',$name)->order_by('grade', 'DESC')->limit(1)->get('imt_1_news_comment_data_0')->row()->grade;        //查询答题表中分数大于当前用户分数的人数
        $sum = count($this->db->where('grade > ',$grade)->order_by('grade', 'DESC')->limit(1)->get('imt_1_news_comment_data_0')->result());
        $rank_num = dr_var("rank_num");
        $sql = "select distinct username,head_img,grade,rewards from imt_1_news_comment_data_0 a where grade=(select max(grade) from imt_1_news_comment_data_0 where username=a.username)  order by grade DESC,inputtime DESC limit ".$rank_num;
        $return = $this->db->query($sql)->result();
        exit(json_encode(array('code'=>1,'msg'=>'拉取答题排行榜前'.$rank_num.'名成功!','my_grade'=>$grade,'my_rank'=>$sum+1,'data'=>$return)));
    }


 由于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',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(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');

 

(我不怕千万人阻挡,只怕自己投降!)


全文详见:http://it-club.cn/post/201.html

TOP

评论列表


发表评论
来宾的头像

TOP

网站分类

TOP

站点信息

  • 文章总数:738
  • 页面总数:1
  • 分类总数:3
  • 标签总数:24
  • 评论总数:270
  • 浏览总数:10021107