重复数据去重,取创建时间最晚的一条。
思路:按重复的字段通过group by 去重,重复的数据通过GROUP_CONCAT()函数收集,再通过SUBSTRING_INDEX()函数截取即可。
实例:
SELECT
UserName,
//字段值按创建时间降序排列,按逗号截取取第一个字段值
SUBSTRING_INDEX( GROUP_CONCAT( new_value ORDER BY create_time DESC ), ',', 1 ) new_value,
SUBSTRING_INDEX( GROUP_CONCAT( create_time ORDER BY create_time DESC ), ',', 1 ) create_time
FROM
user_log // 表名
WHERE//筛选条件自定义
( create_time LIKE '2024-06%' OR create_time LIKE '2024-07%' )
GROUP BY
UserName//需去重的字段
ORDER BY
create_time DESC