MYSQL删除给定日期的第一个和最后一个以外的所有记录

时间:2017-03-30 20:19:41

标签: mysql database bash

这为我提供了特定交换机和端口的所有记录。我想删除除了第一天和最后一天之外的所有内容。

docker run -v /var/lib/mysql mariadb

4 个答案:

答案 0 :(得分:2)

我不知道删除所有这些记录需要多长时间,但我认为 分钟的工作只是等待它:

delete fb from collector.fibre fb 
INNER JOIN 
(
SELECT 
  distinct 
  f.id -- extract only the ids 
FROM 
  collector.fibre  f
  INNER JOIN (
    -- create a special table   day   min_time  max_time 
    select 
      date(time) as 'day',
      min(time) as 'min_time',
      max(time) as 'max_time'
    from 
      collector.fibre
     Where
        -- must be the same set of records 
        -- because we look for a dates of this switch
       fabric_switch_name = 'switch-1' 
       AND port = '0' 
       AND datatype = 'TxElements'
    group by date(time)
  )  fd on date(f.time) = fd.day  -- join the two table to have a special table see example down  
WHERE 
  fabric_switch_name = 'switch-1' 
  AND port = '0' 
  AND datatype = 'TxElements' 
  -- extract record witch the time is not
  -- the min or the max of that date 
  AND (f.time <> fd.min_time and f.time <> fd.max_time) 
) recs on fb.id = recs.id -- join the table and the result of query by the id now you will remove the records that exist in the result of the query

答案 1 :(得分:1)

由&#34; 第一个和最后一个&#34;我会假设你的意思是 最早和最新的 ,所以:

  • 在给定日期中选择条件,然后取消选择最小日期和最大日期值。
  

假设您的时间存储在DATETIME列中。

    DELETE FROM collector.fibre 
         WHERE fabric_switch_name = 'switch-1' 
         AND port = '0' 
         AND datatype = 'TxElements' 
         AND (TIME(time) != 
             (
             SELECT MAX(TIME(time))
             FROM `collector`.`fibre` WHERE fabric_switch_name = 'switch-1' 
             AND port = '0' 
             AND datatype = 'TxElements'
             AND DATE(time) = '2016-05-30'
             )
             AND
             TIME(time) != (
             SELECT MIN(TIME(time))
             FROM `collector`.`fibre` WHERE fabric_switch_name = 'switch-1' 
             AND port = '0' 
             AND datatype = 'TxElements'
             AND DATE(time) = '2016-05-30'
             )
         ) 
         AND DATE(time) = '2016-05-30'

这可能编写得很糟糕,不幸的是,子查询使用标准重复,但概念的工作原理如下:

  

SubSelect by criteria和 Not 行,其中时间列是MAX或MIN时间值符合所述条件

参考:https://stackoverflow.com/a/7836444/3536236

因此,您可以输入每一天并删除非最早和非最新值。如果你想一举遍历整个桌子,我想你想创建一个Procedure,但我不知道如何在这里帮助你。但是你的标题是MYSQL delete all records except first and last for a given date,这意味着你将根据需要一次手动运行一个日期。

实施例

示例设置:

   fabric_switch_name |   datatype  |  port  |        time
 -------------------------------------------------------------------
       switch-1       |  TxElements |    2   |  2016-05-31 15:00:00
       switch-1       |  TxElements |    1   |  2016-05-30 22:10:00
       switch-2       |  TxElements |    0   |  2016-05-30 15:00:00
       switch-1       |  TxElements |    0   |  2016-05-29 10:20:10
       switch-1       |  TxElements |    0   |  2016-05-29 05:50:00
       switch-1       |  TxElements |    0   |  2016-05-29 19:50:00
       switch-1       |  TxElements |    5   |  2016-05-29 21:50:00
       switch-1       |  TxElements |    0   |  2016-05-29 20:11:40

结果集:

   fabric_switch_name |   datatype  |  port  |        time
 -------------------------------------------------------------------
       switch-1       |  TxElements |    2   |  2016-05-31 15:00:00
       switch-1       |  TxElements |    1   |  2016-05-30 22:10:00
       switch-1       |  TxElements |    0   |  2016-05-29 05:50:00
       switch-2       |  TxElements |    0   |  2016-05-30 15:00:00
       switch-1       |  TxElements |    5   |  2016-05-29 21:50:00
       switch-1       |  TxElements |    0   |  2016-05-29 20:11:40

答案 2 :(得分:0)

尝试一下:

DELETE 
FROM collector.fibre  
WHERE fabric_switch_name = 'switch-1'
AND port = '0' 
AND datatype = 'TxElements'
AND time BETWEEN '2016-05-31 00:00:00' AND '2016-05-31 23:59:59' 
AND time NOT IN (
    SELECT MIN(time), MAX(time)
    FROM collector.fibre  
    WHERE fabric_switch_name = 'switch-1'
    AND port = '0' 
    AND datatype = 'TxElements'
    AND time BETWEEN '2016-05-31 00:00:00' AND '2016-05-31 23:59:59' 
)

答案 3 :(得分:0)

此查询将提取应删除的所有记录:

SELECT 
  * 
FROM 
  collector.fibre  f
  INNER JOIN (
    -- create a special table   day   min_time  max_time 
    select 
      date(time) as 'day',
      min(time) as 'min_time',
      max(time) as 'max_time'
    from 
      collector.fibre
     Where fabric_switch_name = 'switch-1'
    group by date(time)
  )  fd on date(f.time) = fd.day  -- join the two table to have a special table see example down  
WHERE 
  fabric_switch_name = 'switch-1' 
  AND port = '0' 
  AND datatype = 'TxElements' 
  -- extract record witch the time is not
  -- the min or the max of that date 
  AND (f.time <> fd.min_time and f.time <> fd.max_time) 

让我们假设这是你的日期

    1           2016-05-31 23:00:00 
    2           2016-05-31 01:00:00
    3           2016-05-31 00:00:00
    4           2016-05-30 10:00:12
    5           2016-05-30 01:00:00
    6           2016-05-30 00:05:00

加入的结果:

    1           2016-05-31 23:00:00         2016-05-31   2016-05-31 00:00:00  2016-05-31 23:00:00  -- this will not be selected in the result
    2           2016-05-31 01:00:00         2016-05-31   2016-05-31 00:00:00  2016-05-31 23:00:00  -- this will be selected because it's not min or max of that day
    3           2016-05-31 00:00:00         2016-05-31   2016-05-31 00:00:00  2016-05-31 23:00:00 
    4           2016-05-30 10:00:00         2016-05-30   2016-05-30 00:05:00  2016-05-30 10:00:12
    5           2016-05-30 01:00:00         2016-05-30   2016-05-30 00:05:00  2016-05-30 10:00:12
    6           2016-05-30 00:00:00         2016-05-30   2016-05-30 00:05:00  2016-05-30 10:00:12