引言:php
前兩天業務涉及到一個拉取答題排行榜的需求,數據庫裏數據是這樣的:sql
同一我的可能提交過屢次成績,因此同一我的可能會有屢次記錄;數據庫
同一我的提交的屢次成績中可能有至少兩次成績是同樣的。json
因而,查詢的時候,首先查詢出每一個人的最高成績記錄,而後若是某我的的最高成績記錄有多條,去重!數組
最終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
//通常查詢 $this->db->select('name,grade')->where('sex','男')->limit(10,10)->get('tableName')->result();
1、查詢結果集spa
->result();---------------------------------------------------返回object數組(多條記錄)code
->result_array();-------------------------------------------返回二維數組對象
->row();------------------------------------------------------返回一個對象(一條記錄)
->row_array();----------------------------------------------返回一維數組
$this->db->insert_id();------------------------------------上一條插入的數據記錄的id值(數據插到主表後,當即插到附表的話,id關聯的時候回用到)
2、條件查詢
->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%
3、distinct去重
->select('username,grade')->distinct();---------------根據用戶名和成績去重,只有用戶名和成績都相同纔會被去重
4、排序
->order_by('title DESC, name ASC');-----------------title降序name升序
->order_by('name','RANDOM');------------------------隨機排序,第一個參數無實際意義,可是要寫,隨機嘛,談不上根據哪一個字段隨機了
5、分頁
->limit(10);---------------------------------------------------未設置偏移量,那麼默認偏移量爲0,即從0開始選擇,選出10條數據
->limit(10,10);-----------------------------------------------設置了偏移量,從10開始,選出10條數據,即11-20條記錄
6、計數
->count_all('tableName')---------------------------------查詢數據表中總的記錄數
->where($where)->from('tableName')->count_all_results();查詢符合當前條件的記錄的數目,注意用from而不是get
->where($where)->count_all_results('tableName');效果同上
7、插入記錄
$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);--------------------往表中插入多條記錄
8、更新
$data = array(
'title' => 'My title',
'name'=>'My name',
);
->where('id',5)->updata('tableName',$data);--------更新主鍵(id)爲5的記錄的相關數據
9、刪除
$this->db->where('id', $id);
$this->db->delete('mytable');