gpt4 book ai didi

mysql - 删除重复号码状态存在吗?

转载 作者:可可西里 更新时间:2023-11-01 08:05:22 24 4
gpt4 key购买 nike

如何将重复项删除到新表中?

例如表中:

id |   number      |    status    | statusdate 
1 | 12121312 | Processing | 01/06/2015 12:32:00
2 | 12121312 | Processing | 02/06/2015 13:37:00
3 | 12121312 | Processing | 03/06/2015 14:29:00
4 | 99999999 | Processing | 03/06/2015 15:33:00
5 | 99999999 | Completed | 03/06/2015 18:39:00
6 | 99999999 | Completed | 04/06/2015 19:39:00
  1. id 1 到 3 有重复的数字,但没有“已完成”状态,所以从最早的日期开始获取数字

  2. id 4到6,有重复的数字,但“已完成”状态确实存在。获取最新日期的号码

在新表中,应该是这样的:

id |   number      |    status    | statusdate 
1 | 12121312 | Processing | 01/06/2015 12:32:00
6 | 99999999 | Completed | 04/06/2015 19:39:00

如何做到这一点?

最佳答案

这是一个未优化的版本:

DELETE FROM table_name as T
WHERE (
number in (SELECT number
FROM table_name
WHERE status = 'Completed')
AND statusdate < (SELECT MAX(statusdate)
FROM table_name
GROUP BY number
HAVING number = T.number)
)
OR
(
number in (SELECT number
FROM table_name
WHERE status = 'Processing')
AND statusdate > (SELECT MIN(statusdate)
FROM table_name
GROUP BY number
HAVING number = T.number)
);

这些是它删除的记录:

id  number      status      statusdate
2 12121312 Processing February, 06 2015 13:37:00
3 12121312 Processing March, 06 2015 14:29:00
4 99999999 Processing March, 06 2015 15:33:00
5 99999999 Completed March, 06 2015 18:39:00
6 99999999 Completed April, 06 2015 19:39:00

只需将 Fiddle 中的 SELECT * FROM 替换为 DELETE FROM .

关于mysql - 删除重复号码状态存在吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31514710/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com