我是靠谱客的博主 勤劳板栗,最近开发中收集的这篇文章主要介绍mysql按日期删除,如何删除MySQL中的记录并保持最新日期,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

Example table_1

ID Email Answer UpdateDate

1 xxx.@xx.com 1 2011-07-02

2 xxx.@xx.com 3 2011-07-11

3 vvv.@xx.com 3 2011-07-12

4 vvv.@xx.com 5 2011-07-13

5 xxx.@xx.com 5 2011-07-14

6 xxx.@xx.com 4 2011-07-14

7 xxx.@xx.com 4 2011-07-14

8 zzz.@xx.com 4 2011-07-15

How do I delete this records but keep the latest UpdateDate

And the result :

ID Email Answer UpdateDate

4 vvv.@xx.com 5 2011-07-13

7 xxx.@xx.com 4 2011-07-14

8 zzz.@xx.com 4 2011-07-15

解决方案

You could use a temporary variable to store the highest date and then a seperate query to delete everything that is < than that. Remember that variables are connection specific.

select max(UpdateDate) from table_1 into @TempUpdateDate

delete from table_1 where UpdateDate < @TempUpdateDate

最后

以上就是勤劳板栗为你收集整理的mysql按日期删除,如何删除MySQL中的记录并保持最新日期的全部内容,希望文章能够帮你解决mysql按日期删除,如何删除MySQL中的记录并保持最新日期所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(43)

评论列表共有 0 条评论

立即
投稿
返回
顶部