我检查了SO,但没有找到解决方法。
我正在使用CodeIgniter。
我有一个具有相同member_id的表。我正在做的是我必须显示所有member_id,但是如果member_id相同,则显示该成员ID的最后一条记录。我在下面的查询中进行了尝试,但仍然无法为我工作。
如何通过添加的最后一条记录进行分组?
这是桌子
现在注意,这里的member_id 337和343在表中是两次,我得到的第一条记录倒数第二条。
请检查此查询
SELECT MAX(member_id),membershipForTheYear,membership_added_date
FROM membership_details GROUP BY member_id
ORDER BY membership_added_date DESC
我正在使用phpmyadmin。我导出文件,将其打开并在此处添加代码。
CREATE TABLE `membership_details` (
`membership_id` int(11) NOT NULL,
`member_id` int(11) NOT NULL,
`membership_added_date` varchar(100) NOT NULL,
`membershipForTheYear` varchar(100) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
INSERT INTO `membership_details` (`membership_id`, `member_id`, `membership_added_date`, `membershipForTheYear`) VALUES
(1, 209, '23-02-2019 02:40:22', '2018-2019'),
(2, 337, '22-02-2019 02:47:10', '2018-2019'),
(3, 2, '23-02-2019 06:36:40', '2019-2020'),
(4, 337, '23-02-2019 21:15:08', '2019-2020'),
(5, 343, '24-02-2019 15:07:05', '2018-2019'),
(6, 343, '24-02-2019 15:09:20', '2019-2020');
我尝试查询
SELECT *
FROM membership_details WHERE (membership_id,member_id) IN
( SELECT membership_id, MAX(membership_id)
FROM membership_details
GROUP BY member_id
)
CodeIgniter
/*sub query*/
$this->db->select('member_id, membershipForTheYear, membership_added_date, max(membership_id) as membership_id')->from('membership_details')->group_by('member_id');
$subQuery = $this->db->get_compiled_select();
/*end sub query*/
$get_s_member = array('members.member_type' =>2,'members.is_Approved'=>1,'members.is_status'=>1,'relation_member.primary_customer_id' =>$gotPrimaryCustid);
$this->db->select('*');
$this->db->from('members');
$this->db->join('('.$subQuery.') as membership_details', 'members.member_id = membership_details.member_id','LEFT');
$this->db->where($get_s_member);
$query = $this->db->get();
$result = $query->result();
// print_r($result);
echo $this->db->last_query();
if($result)
{
return $result;
}
else
{
return 0;
}
答案 0 :(得分:2)
member_id
的最大值(也称为最后一个)membership_id
。membership_id
的上述查询派生表对当前表进行内部联接,并显示详细信息。 SQL:
select m1.membership_id,m1.member_id,m1.membership_added_date,m1.membershipForTheYear
from membership_details m1
inner join (select member_id,max(membership_id) as membership_id
from membership_details
group by member_id) m2
on m1.membership_id = m2.membership_id;
答案 1 :(得分:1)
如果membership_id
是AUTO_INCREMENT PRIMARY KEY
,并且可以选择其中值最高的行,则可以使用:
SELECT *
FROM membership_details
NATURAL JOIN (
SELECT MAX(membership_id) as membership_id
FROM membership_details
GROUP BY member_id
) sub
您还可以修复已经尝试的方法,即:
SELECT *
FROM membership_details WHERE (membership_id,member_id) IN
( SELECT membership_id, MAX(membership_id)
FROM membership_details
GROUP BY member_id
)
将其更改为:
SELECT *
FROM membership_details WHERE (membership_id,member_id) IN
( SELECT MAX(membership_id), member_id
FROM membership_details
GROUP BY member_id
)
但是如果membership_id
是主键,则不需要在WHERE子句中使用member_id
,并且以下内容也应该起作用:
SELECT *
FROM membership_details WHERE (membership_id) IN
( SELECT MAX(membership_id)
FROM membership_details
GROUP BY member_id
)
如您所见,子查询与我的JOIN查询相同。