select指定服务ID之前的所有数据

select all data before a specify service ID

早上好!

我已经为您创建了这个 fiddle: https://dbfiddle.uk/?rdbms=mariadb_10.5&fiddle=4e2bdf9aecca8b69d829770589c14807

解释:

  1. 创建一个table“时间段”
  2. 用从明天到 + 2 天的日期范围填充此 table,时间范围。 14-18点。
  3. 更新 3 次批次并将服务设置为“2”(我选择了随机 ID)
  4. SELECT * FROM timeslots ORDER BY timeslot ASC

这是结果,显示 fiddle。 但现在我需要扩展我的 sql 查询以获得特定结果,但我不知道如何实现它。也许你有一些想法。

第一步: 我只想获得 service = 0:

的时隙

对我来说很简单:

SELECT * FROM timeslots WHERE service = 0

部分结果:

2021-07-06 14:00:00 0
2021-07-06 15:00:00 0
2021-07-06 16:00:00 2 (not this value, because service is not 0)
2021-07-06 17:00:00 0

一切顺利!

但是如果存在 - 在同一天 - 服务 2 的时间段(如上面的结果 2021-07-06 16:00:00)我只需要 2021-07-06 [=46 之后的值=].在此示例中,结果应为:

2021-07-06 17:00:00

希望您能理解我的问题并能帮助我! 谢谢!

您正在查找 NOT EXISTS,因为您想查看 不存在的行 同一天或之后的服务二时段。

select *
from timeslots ts
where not exists
(
  select null
  from timeslots ts2
  where date(ts2.timeslot) = date(ts.timeslot)
  and time(ts2.timeslot) >= time(ts2.timeslot)
  and service = 2
)
order by timeslot;

演示:https://dbfiddle.uk/?rdbms=mariadb_10.5&fiddle=6cf862d80445fcda030e2be35adb4909