我有这张 table
我想创建一个存储过程,该过程将删除所有记录,但只保留每个 Application_Id 和External_Account_Id 的最新记录(具有最新的Last_Warning_Message)。所以执行存储过程后,表应该是这样的:
最佳答案
在MySQL中,您可以在删除
中使用join
:
delete t
from thistable t join
(select Application_Id, External_Account_Id,
max(Last_Warning_Message) as max_Last_Warning_Message
from thistable t
group by Application_Id, External_Account_Id
) tt
on t.Application_Id = tt.Application_Id and
t.External_Account_Id = tt.External_Account_Id and
t.Last_Warning_Message < tt.max_Last_Warning_Message;
关于mysql - 如何在按组和日期排序后删除除一行之外的所有 MySQL 行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50552078/