delete from
tablename
where
(
date_work >= startdate
and date_work < (
select
IFNULL(date_work, 999999) as tmpdw
from
tablename
where
date_work > startdate
and date_type <> otpusktype
and ROUND(
DATEDIFF(
tmpdw,
(
select
date_work
from
tablename
where
date_work < tmpdw
order by
date_work DESC
LIMIT
1
)
)
) > 1
order by
date_work
LIMIT
1
)
)
delete from tablename
where (date_work => startdate
and date_work < (select top 1 IFNULL(date_work, datetime_max_value) as date_work from tablename
where date_work > startdate and date_type <> otpusktype order by date_work))