? ? ? ? ? 发生场景:用户表被删除,根据用户日志操作最后一条数据修复用户表
SELECT
*
FROM
(
SELECT
UserID,
Score,
MAX(ID) AS MaxLoginDate
FROM
user_inout_log_202312
GROUP BY
UserID
ORDER BY
ID DESC
) t
ORDER BY
Score DESC;
结果:
根据数据验证这个结果是错误的,不是我们想要的数据
SELECT UserID,Score,Diamond,Lottery from (
SELECT
user_inout_log_202312.*
FROM
user_inout_log_202312
INNER JOIN (
SELECT
UserID,
MAX(ID) AS MaxLoginDate
FROM
user_inout_log_202312
GROUP BY
UserID
) AS latest ON user_inout_log_202312.UserID = latest.UserID
AND user_inout_log_202312.ID = latest.MaxLoginDate ORDER BY Score desc
) t ;
子查询获取最新的用户id,要user_inout_log_202312 的全部数据 用inner join 获取重复的数据
经过验证第二条sql 使我们想要的结果