我有这个:
+----+---------------------+ | id | date | +----+---------------------+ | 1 | 2017-01-01 12:30:00 | | 2 | 2017-01-01 12:30:00 | | 3 | 2017-01-02 00:00:00 | | 4 | 2017-01-03 00:00:00 | +----+---------------------+
这是大型数据集的一部分(具有各种连接)。
我想删除具有重复日期的条目,同时保留其中一个,即:
+----+---------------------+ | id | date | +----+---------------------+ | 1 | 2017-01-01 12:30:00 | | 3 | 2017-01-02 00:00:00 | | 4 | 2017-01-03 00:00:00 | +----+---------------------+
有没有人知道一个好的方法。我曾想过排序然后遍历每一行并根据之前是否遇到过日期进行删除。或者在日期上使用 COUNT/HAVING。
谢谢。
最佳答案
你可以使用聚合:
select min(id) as id, date
from t
group by date;
如果您有额外的列,则效果不佳。相反,在 where
子句上使用过滤器:
select t.*
from t
where t.id = (select min(t2.id) from t t2 where t2.date = t.date);
关于MySQL - 根据相同日期条目删除重复项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42068306/