删除记录

发布于 2024-11-17 10:49:31 字数 598 浏览 2 评论 0原文

我有一个表 [user_logs] ,其中包含以下字段 [username]、[datetimelog]

示例数据

==============
user1   2011-06-28 08:49:01
user2   2011-06-28 08:59:38
user3   2011-06-28 09:04:31
user4   2011-06-28 10:00:15
user2   2011-06-28 10:28:54
user1   2011-06-29 08:31:22
user9   2011-06-29 08:32:32
user2   2011-06-29 10:13:53
user1   2011-06-29 13:11:15

我想知道如何创建 SQL 删除查询来删除除最后一个日志之外的所有用户日志,以便上面的示例将在之后生成以下内容删除查询

user1   2011-06-29 13:11:15
user2   2011-06-29 10:13:53
user3   2011-06-28 09:04:31
user4   2011-06-28 10:00:15
user9   2011-06-29 08:32:32

I have a table [user_logs] with the following fields [username], [datetimelog]

Sample Data

==============
user1   2011-06-28 08:49:01
user2   2011-06-28 08:59:38
user3   2011-06-28 09:04:31
user4   2011-06-28 10:00:15
user2   2011-06-28 10:28:54
user1   2011-06-29 08:31:22
user9   2011-06-29 08:32:32
user2   2011-06-29 10:13:53
user1   2011-06-29 13:11:15

I want to know how to create an SQL Delete query to delete all user logs EXCEPT their last log so that the above example will produce the following after a DELETE query

user1   2011-06-29 13:11:15
user2   2011-06-29 10:13:53
user3   2011-06-28 09:04:31
user4   2011-06-28 10:00:15
user9   2011-06-29 08:32:32

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

帅哥哥的热头脑 2024-11-24 10:49:31

怎么样:

DELETE FROM 
    MY_TABLE M -- delete from the table
LEFT JOIN
    MY_TABLE M2 ON M.user = M2.user -- table needs to be joined TO ITSELF
WHERE
    NOT M.LOG_DATE = MAX( M2.LOG_DATE ) -- Anything which isn't MAX goes.

这可行吗?

What about:

DELETE FROM 
    MY_TABLE M -- delete from the table
LEFT JOIN
    MY_TABLE M2 ON M.user = M2.user -- table needs to be joined TO ITSELF
WHERE
    NOT M.LOG_DATE = MAX( M2.LOG_DATE ) -- Anything which isn't MAX goes.

Could that work?

回忆躺在深渊里 2024-11-24 10:49:31
DELETE FROM table a WHERE time != (SELECT MAX(time) FROM table b WHERE b.user=a.user);

这里删除一行,如果不是同一user_id组内的最长时间

DELETE FROM table a WHERE time != (SELECT MAX(time) FROM table b WHERE b.user=a.user);

Here delete a row, if its not the maximum time in group with the same user_id

天荒地未老 2024-11-24 10:49:31
DELETE from user_logs UL1, user_logs UL2
where UL1.username =UL2.datetimelog
and UL1.datetimelog < UL2.datetimelog

尝试一下

DELETE from user_logs UL1, user_logs UL2
where UL1.username =UL2.datetimelog
and UL1.datetimelog < UL2.datetimelog

Try that

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文