Java MySQL删除重复日期字段上的查询

时间:2022-11-16 04:26:15

I am trying to delete few entries from DB table based on a date filed.

我试图根据提交的日期从DB表中删除一些条目。

For example, consider the following data.

例如,请考虑以下数据。

 - Program Name -------    Job_Run_date 
 - program.exe  -------    2015-08-04     
 - program.exe  -------    2015-08-04    
 - program.exe  -------    2015-08-05      
 - program.exe  -------    2015-08-05      
 - program.exe  -------    2015-08-06

in the above list, the entry for 2015-08-04 occurred twice. The expected number of entries for each date field are only one per date. I would be storing each and every run information in a DB table, however I wouldn't be needing multiple entries for a program. But an entry is required to identify the processes, which have run on a particular date.

在上面的列表中,2015-08-04的条目发生了两次。每个日期字段的预期条目数仅为每个日期一个。我将每个运行信息存储在数据库表中,但是我不需要为程序输入多个条目。但是需要输入一个条目来识别在特定日期运行的进程。

I have tried to run the following kind of queries from a java program, but some how the logic doesn't seem to work.
DELETE FROM PROCESS_DATA WHERE RUN_Date NOT IN ( SELECT RUN_Date FROM ( SELECT MIN(RUN_Date) FROM PROCESS_DATA GROUP BY RUN_Date))

我试图从java程序运行以下类型的查询,但有些逻辑似乎不起作用。 DELETE FROM PROCESS_DATA WHERE RUN_Date NOT IN(SELECT RUN_Date FROM(SELECT MIN(RUN_Date)FROM PROCESS_DATA GROUP BY RUN_Date))

Any modifications to the query is taking a lot of time to run and couldn't find much info with respect to this specific case.

对查询的任何修改都需要花费大量时间才能运行,并且无法找到有关此特定情况的大量信息。

1 个解决方案

#1


There are two options to fix this problem,

有两种方法可以解决这个问题,

1. Before insertion, you can validate if there exists any record for this date.
2. Execute the below query
DELETE FROM PROCESS_DATA WHERE RUN_Date NOT IN ( SELECT distinct RUN_Date FROM PROCESS_DATA )

#1


There are two options to fix this problem,

有两种方法可以解决这个问题,

1. Before insertion, you can validate if there exists any record for this date.
2. Execute the below query
DELETE FROM PROCESS_DATA WHERE RUN_Date NOT IN ( SELECT distinct RUN_Date FROM PROCESS_DATA )