表user
左连接表personal_record
然后加入表activity
。
user
table:id ...
personal_record
table:user.id,activity.id,confirm_time ...
activity
:id,time,delete_time ...
注意:时间是指花在此活动上的时间,等等。
在user
加入personal_record
之前,部分persanoal_record
行需要过滤为personal_record
仅在设置confirm_time时有效。personal_reocrd
仅包含activity_id, activity
其中包含的活动'时间需要加入到一个人花的总和(时间)。
通过更新activity.delete_time
列来删除活动。
这是我的代码。
更新(几乎已解决):( $ data_begin过滤记录范围)
$result = $this->db->select('user.*, ifnull(sum(ac.time), 0) as time')
->from('user')
->join('(select * from personal_record where personal_record.confirm_time is not null) as pr','user.id = pr.person', 'left')
->join('(select * from activity where activity.delete_time is null and activity.create_time >"'.$date_begin.'")as ac','ac.id = pr.activity', 'left')
->group_by('user.id')
->order_by('time', 'desc')
->get()
->result();
答案 0 :(得分:1)
$this->db->select('user.*,personal_record.*,ifnull(sum(activity.time), 0) as time');
$this->db->from('user');
$this->db->join('personal_record','personal_record.person = user.id');
$this->db->join('activity','activity.id = personal_record.activity');
$this->db->where('activity.delete_time',null);
$this->db->or_where('personal_record.confirm_time !=', null);
$this->db->order_by('time', 'asc');
$this->db->group_by('user.id');
$data = $this->db->get()->result();
return $data;