引言:
前兩天業務涉及到一個拉取答題排行榜的需求,數據庫里數據是這樣的:
同一個人可能提交過多次成績,所以同一個人可能會有多次記錄;
同一個人提交的多次成績中可能有至少兩次成績是一樣的。
於是,查詢的時候,首先查詢出每個人的最高成績記錄,然后如果某個人的最高成績記錄有多條,去重!
最終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');