SQL通过一列选择唯一值,而在另一列中选择最新值

问题描述 投票:0回答:1

我具有以下表格结构:

leads:

name  | email | date
-------------------------------------------------------
John  | [email protected]  | 2020-01-21
J     | [email protected]  | 2020-01-20
Alex  | [email protected]  | 2020-01-19
A     | [email protected]  | 2020-01-18
James | [email protected] | 2020-01-17

我只需要选择带有UNIQUE电子邮件和最新关联名称的行,这样预期的结果是:

name  | email | date
-------------------------------------------------------
John  | [email protected]  | 2020-01-21
Alex  | [email protected]  | 2020-01-19
James | [email protected] | 2020-01-17
mysql sql greatest-n-per-group
1个回答
3
投票

不存在使用

select * from tbl a 
where not exist(select 1 from tbl b where a.email = b.email  and a.date < b.date)
© www.soinside.com 2019 - 2024. All rights reserved.