要实现这一点,您确实需要将 DATE + VARCHAR 字段转换为更合适的内容,例如 DATETIME(我更喜欢日期时间,它在查询时更具可读性,并且在内部与时间戳一样快)
然后,在您将 4 个字段合并为 2 个字段后,您可以使用两种类型的查询:
SELECT
CASE
WHEN startDateTime <= "targetDate" AND endDateTime > "targetDate" THEN "while"
WHEN startDateTime > "targetDate" THEN "before"
WHEN endDateTime <= "targetDate" THEN "after"
END AS timeframe,
events.*
FROM
events
或者你也可以使用联合,两者都会产生相似的结果,选择你喜欢的一个:
SELECT "before" AS timeframe, events.* FROM events
WHERE startDateTime > "targetDate"
UNION
SELECT "while" AS timeframe, events.* FROM events
WHERE startDateTime <= "targetDate" AND endDateTime > "targetDate"
UNION
SELECT "after" AS timeframe, events.* FROM events
WHERE endDateTime <= "targetDate"
PS:我没有测试它,但这个想法是正确的,你可能需要适应它。
祝你好运