CI多表查询
2024-11-25 10:12:39
> ### 常用查询
private function _where(&$select, $data) {
$select = $this->link->select('count(*) as total');
//指定日期的当天内
if(isset($data['startTime'])){
$select->where('DATEDIFF(from_unixtime(startTime),from_unixtime('.$data['startTime'].'))=0');
}
//统计当天数据
$select->where('DATEDIFF(from_unixtime(startTime),now())=0')
$where = "MOD(ROUND(UNIX_TIMESTAMP(time)/60,0),{$time})=0";
//where_in
$select->where_in('id', $id);
//开始分页
$select = $this->link->limit(SITE_ADMIN_PAGESIZE, SITE_ADMIN_PAGESIZE * ($page - 1));
$data = $select->order_by($_order)->get($this->prefix)->result_array();
return $data;
}
//join查询
$this->db
->from('member AS a')
->join('member_data AS b', 'a.uid=b.uid', 'left')
->where('a.uid', $uid)
->limit(1)
->get()
->row_array();
$this->db->query("SELECT * FROM `MT_Game_red` AS `a` LEFT JOIN `MT_Game_red_log` AS `b` ON `a`.`uid`=`b`.`uid` WHERE `a`.`uid` = {$this->uid} limit {$offset},{$this->pagesize}")->result_array();
$this->db->where('uid', 1)->set('money', 'money-'.$money, FALSE)->set('spend', 'spend+'.$money, FALSE)->update('member');
```
> ### 任意字段查询+分页
```
public function order_list(){
$table = 'mt_1_order_1';
$total = (int)$this->input->get('total');
$param['status'] = $this->input->get('status');
$param['uid'] = $this->uid;
$list = array();
if (!$total) {
$db = $this->db;
$this->_where($db, $param);
$data = $db->select('count(*) as total')->get($table)->row_array();
$total = $data['total'];
}
$page = max((int)$this->input->get('page'), 1);
$db = $this->db;
$this->_where($db, $param);
$list = $db->order_by('time desc')->limit(10, 10 * ($page - 1))->get($table)->result_array();
$this->template->assign(array(
'list' => $list,
'pages' => $this->get_pagination(dr_url('order/home/order_list', array('status'=>$status, 'uid'=>$uid)), $total),
));
$this->template->display('order_list.html');
}
// 多字段搜索拼接
protected function _where(&$select, $param){
foreach ($param as $key=>$value) {
isset($param[$key]) && $param[$key] && $select->where($key, $value);
}
} 