zoukankan      html  css  js  c++  java
  • 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');
    

      

  • 相关阅读:
    前端之HTML补充
    前端之HTML
    mysql 视图,触发器,存储
    mysql 函数 事务
    索引扩展
    mysql数据库索引相关
    mysql 存储过程查询语句
    mysql 单表查询
    mysql 多表连接查询
    js引入的几种简单写法
  • 原文地址:https://www.cnblogs.com/T8888/p/12058303.html
Copyright © 2011-2022 走看看