原始帖子标题: 查询从字段中删除重复值 关于表格列中最大值的存在 保持最近的
我有一个名为Idle_info的表:列是
open_date(date/time),time_from(date/time),duration(number),user(text)
如果open_date和time_from相同,我想删除这些值。只有在持续时间最长的情况下才会保存该值。
我试过了:
DELETE FROM Idle_info
WHERE idle_duration NOT IN (SELECT MAX(i.idle_duration)
FROM Idle_info i
WHERE i.open_date = open_date AND i.time_from=time_from AND i.user=user
GROUP BY i.open_date,i.time_from,i.idle_duration,i.user
但没有工作
感谢
答案 0 :(得分:1)
这是您的查询:
DELETE FROM Idle_info
WHERE idle_duration NOT IN (SELECT MAX(i.idle_duration)
FROM Idle_info i
WHERE i.open_date = open_date AND i.time_from = time_from AND
i.user = user
GROUP BY i.open_date,i.time_from, i.idle_duration, i.user
)
我的猜测是,您希望保留一组给定条件的最新记录。你有两个问题。一个是缺乏相关性。第二个是group by
子句。我相信以下是你想要的:
DELETE FROM Idle_info i
WHERE i.idle_duration < (SELECT MAX(i.idle_duration)
FROM Idle_info i2
WHERE i.open_date = i2.open_date AND
i.time_from = i2.time_from AND
i.user = i2.user
);
答案 1 :(得分:0)
试试这个:
DELETE FROM Idle_info r1
WHERE exists (select 1
from idle_info r2
where r1.user=r2.user
and r1.time_from=r2.time_from
and r1.open_date=r2.open_date
and r1.idle_duration<r2.idle_duration
)