我正在使用CodeIgniter。我有两个表,我要加入,而不是使用加入。
成员
member_id | name | email
1 |lkjh |asd@gmail.com
2 |jjlk |qwe@gmail.com
3 |sfsd |gfh@gmail.com
4 |qqwe |tyut@gmail.com
member_details
id | member_id |game | year | date_of_added
1 | 1 |lkjh |2018-2019 | 14-02-2019 02:26:30
2 | 1 |jjlk |2019-2020 | 14-02-2019 03:30:20
3 | 2 |sfsd |2018-2019 | 14-02-2019 04:00:40
4 | 3 |qqwe |2018-2019 | 14-02-2019 04:30:20
5 | 2 |sfsd |2019-2020 | 14-02-2019 05:45:30
现在,我的问题是,我必须显示member_details中的所有记录,但是我必须显示member_id的最后一条记录。
所以我对member_details表的预期输出是
1 |jjlk |2019-2020 | 14-02-2019 03:30:20
3 |qqwe |2018-2019 | 14-02-2019 04:30:20
2 |sfsd |2019-2020 | 14-02-2019 05:45:30
我尝试了以下代码
$get_s_member = array('members.is_Approved'=>1,'members.is_status'=>1);
$this->db->select('*');
$this->db->from('members');
$this->db->join('membership_details', 'members.member_id = membership_details.member_id','LEFT');
$this->db->where($get_s_member);
$this->db->group_by('membership_details.member_id');
$this->db->order_by('membership_details.date_of_added','DESC');
$this->db->limit('1');
$query = $this->db->get();
$res = $query->result();
return $res;
答案 0 :(得分:0)
select t1.*, t2.* from
member_details t1
join member
on t1.member_id = t2.member_id
where (t1.member_id, t1.date_of_added) in
(select member_id, max(date_of_added)
from member_details
group by member_id
)
答案 1 :(得分:0)
尝试一下
$get_s_member = array('members.is_Approved'=>1,'members.is_status'=>1);
$this->db->select('*');
$this->db->from('membership_details');
$this->db->join('members', 'membership_details.member_id = members.member_id','LEFT');
$this->db->where($get_s_member);
$this->db->group_by('membership_details.member_id');
$this->db->order_by('membership_details.date_of_added','DESC');
$query = $this->db->get();
$res = $query->result();
return $res;