mysql - 如何在按组和日期排序后删除除一行之外的所有 MySQL 行?
问题描述
我有这张桌子
我想创建一个存储过程,它将删除所有记录,但只保留每个 Application_Id 和 External_Account_Id 的最新记录(具有最新的 Last_Warning_Message)。所以执行完存储过程后,表应该是这样的:
解决方案
在 MySQL 中,您可以join
使用delete
:
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;
推荐阅读
- sql - 将本地 SQL Server 数据库迁移到 Azure SQL 数据库
- python - Creating summary table on groupby dataframe based on condition
- javascript - WorldPay AJAX 请求的 CORS/预检错误
- r - R ggplot2 中的 scale_x/y_continuous 被 n 打破?
- jquery - 区分要在jquery中使用的h1标签
- html - Node 红色 HTML 节点以查看 Inspect 而不是 View Source
- mysql - Frm文件位置
- angular - 在具有 ionic3/Angular4 的模型中使用服务/提供者
- java - 当应用程序部署为后台服务(jar)时,spring-boot 提供静态内容
- sql - 如何根据 SQL Server 中的条件派生新列?