sql查出好几条出来时间都重复的记录,怎么保留时间最早的那条,其他的去除
31浏览 · 2024-03-15 14:46:30
你可以使用以下 SQL 语句来查出[时间](https://geek.csdn.net/educolumn/3fb26f40673739b3a46094a10b56bc3a?spm=1055.2569.3001.10083)重复的记录,并保留[时间](https://geek.csdn.net/educolumn/3fb26f40673739b3a46094a10b56bc3a?spm=1055.2569.3001.10083)最早的那条:
DELETE FROM table_name
WHERE id NOT IN (
[select](https://geek.csdn.net/educolumn/5d806cb20badd69e4fa54cbe0d705676?spm=1055.2569.3001.10083) id
FROM (
[select](https://geek.csdn.net/educolumn/5d806cb20badd69e4fa54cbe0d705676?spm=1055.2569.3001.10083) id, MIN(time_column) AS min_time
FROM table_name
GROUP BY column1, column2, ...
HAVING COUNT(*) > 1
) AS temp_table
请替换 `table_name`、`time_column`、`column1`、`column2` 等为你自己的表名和列名。这个语句会删除所有重复记录中[时间](https://geek.csdn.net/educolumn/3fb26f40673739b3a46094a10b56bc3a?spm=1055.2569.3001.10083)不是最早的那条记录。
相关问题
你可以使用以下 SQL 语句来查出多个表中时间重复的记录,并保留时间最早的那条:
DELETE FROM table_name
WHERE id NOT IN (
SELECT id
FROM (
SELECT id, MIN(time_column) AS min_time
FROM table_name
GROUP BY column1, column2, ...
HAVING COUNT(*) > 1
) AS temp_table
```
你可以使用以下 SQL 语句来查询多个表中时间重复的记录,并保留时间最早的那条,并且不显示重复的记录:
SELECT DISTINCT t1.*
FROM table1 t1
JOIN (