by "first and last" I will assume you mean earliest and latest, so:
- Select criteria in the given date and then deselect the minimum date and maximum date values.
It is assumed your time is stored in a DATETIME
column.
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'
This is probably poorly written and does unfortunately use criteria repetition for the subqueries but the concept works as:
SubSelect by criteria and Not rows where the time column is MAX or MIN time values fitting said criteria
Reference : https://stackoverflow.com/a/7836444/3536236
So you can then input each day and remove non-earliest and non-latest values. If you want to cycle through the whole table in one fell swoop I would figure you want to create a Procedure
but I don't know enough to help you much here. However your title says MYSQL delete all records except first and last for a given date
which implies you will manually run it one date at a time as needed.
Example:
Example Set:
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
Result Set:
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